[antlr-interest] Boolean and Arithmetic expression difficulty

Geir Ove Skjaervik geiroves at online.no
Mon Jun 20 01:30:36 PDT 2005


Hello,

This is the answer I posted a few days ago to another user: I think this
grammar example will help you understand how to parse the nested
paranthesis you need to process in your grammar.

Geir Ove

Earlier posting:

The parser is built up from rules: Each rule translates into a method in
the underlying language (Java or C#). Rules refers to other rules. Each
rule processes the structure of your grammar until the final rule
processes the very atoms (leaves) for your grammar: 

A rule has the form:

start
	: PROGRAM^ statementList END
	;


I will assume a trivial grammar of expressions to try to give you an
idea of how nested paranthesis "(" and ")" are handled for you: The same
of course applies to nested curlys.

The clue below is that makes this parser accept expressions with nested
paranthesis, is tha that ultimately, the rule factor has a subrule that
accepts LPAREN expression RPAREN. The parser will recursievly call your
factor and expression rules until all nested paranthesis has been
parsed.

NOTE: LPAREN PLUS and so on are defined in your lexer:

LPAREN : '(' ;




Start
	// NOTE: Importan to have a Start Rule that creates a Tree Root
(the ^ after PROGRM creates a Tree Node: )
	// in this case the Root where the complete parse tree will be
built under.

	: PROGRAM^ statementList END
	;

statmentList
	: (expression SEMI!)*
	;

expression
	: simpleExpression ((GT^ | LT^ | EQUALS^) expression)?  
	;	

simpleExpression throws GeosCalculatorParsingException
	:  term ((PLUS^ | MINUS^) simpleExpression)?
	;

term
	: power ((MUL^ | DIV^) term)?
	;


power
	: factor (POW^ power)? 
	;


factor 
	: NUMBER
		{
			// Insert some action to interpret the number
		}
	| LPAREN^ expression RPAREN! 
	;



Hope there aren't to many errors in the above. And hope it helps,
allthough I feel with you: The ANTLR docs are not for beginners ....


Geir Ove

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Ramon V. del
Rosario
Sent: 13. juni 2005 09:37
To: antlr-interest at antlr.org
Subject: [antlr-interest] newbie: how to use parse rule and nested curly


Hi, am trying to parse bind config file but is
struggling to understand how antlr parse rule work.
right now i'm just using antlr to tokenise the
file....
using a counter to parse nested { {}; }; sort of
structure but has been reading the ref manual and it 
seen the parse rule should be able to parse such a
thing easily. but can't find much notes on how to
create
the parser except the calculator.

can we create a our own object when a parse rule is
confirm? eg when acl block is found then i can

ACL a = new ACL($getText());
while ($hasNext()
	a.addElement($getText());

thanks for reading

----- sample bind config ------
acl xyz-slaves {
160.9.201.3;
};

options {
directory "/etc";
allow-transfer { xyz-slaves; localhost; };
};

--------- t.g ------------
class P extends Parser;

// how do we use the parse rule ???
document : (acl | WORDSTRING) + EOF;

acl : ACL named_block ;
opt : OPT named_block ;

named_block : WORDSTRING LPAREN (named_block |
WORDSTRING)* RPAREN SEMICOLON ;
	
class L extends Lexer;

options {
caseSensitive=false;
caseSensitiveLiterals=false;
k=7;
}

LPAREN : '{' ;

RPAREN : '}' ;

SEMICOLON : ';' ;

ACL : "acl";

OPT : "options";

WORDSTRING : (WORD | STRING)+ ;
						
protected	
STRING : '"'! (~'"')+ '"'! 	
		| '\''! (~'\'')+ '\''! ;
		
protected
WORD : ( ALPHA | INT | SYM )+ ;

protected
ALPHA : ('a'..'z')+ ;

protected
INT : ('0'..'9')+;

protected
SYM : ('.' | '!' | '=' | '_' | '/')+;

WS	:	(	' '
		|	'\t'
		|	'\r' '\n' { newline(); }
		|	'\n' { newline(); }
		)
		{$setType(Token.SKIP);}	
	;


		
__________________________________ 
Discover Yahoo! 
Find restaurants, movies, travel and more fun for the weekend. Check it
out! 
http://discover.yahoo.com/weekend.html 





More information about the antlr-interest mailing list