[antlr-interest] Problems with syntactic predicates(?)
Shmuel Siegel
ssiegel at finjan.com
Sun Jan 27 05:02:39 PST 2008
The question at hand isn't how to write the grammar; we all agree that
it is better to avoid predicates. The question is "why is the original
grammar failing?" As far as I can tell, the DFA predictor is getting
confused by the recursion in "args". If I make args depend on IDENTs
only, I can get the grammar to work (that is, it parses boo(a,b)).
-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Mark Wright
Sent: Sunday, January 27, 2008 1:30 PM
To: antlr-interest at antlr.org
Subject: Re: [antlr-interest] Problems with syntactic predicates(?)
On Sat, 26 Jan 2008 17:06:21 -0800
"Jim Idle" <jimi at temporal-wave.com> wrote:
> Look at the example grammars for how to set up for parsing
> expressions with precedence, you don't need those predicates but you
> do need to chain your rules correctly. Start by coding the primitives
> (the thigns that cannot break down further). You want something like
>
> equations
> : equation* EOF
> ;
>
> equation
> : primary ('=' primary)?
> ;
>
> primary
> : IDENT ('(' expression (',' expression)* ')')?
> | INT
> ;
>
> INT : ( '0'..'9' )+ ;
> IDENT : ( 'a'..'z' )+ ;
> WS : (' ' | '\t' )+ { $channel = HIDDEN; } ;
> NL : '\r'? '\n' { $channel = HIDDEN; } ;
Hello Jim,
I agree with Jim's approach here.
> You are trying to construct context/semantics in the parser, but it's
> job is to parse correct syntactical constructs only. After the parser
> you can apply context and semantics.
You can construct context/semantics in the parser with the
left factoring you are suggesting:
grammar test;
options {
output=AST;
TokenLabelType=CommonToken;
ASTLabelType=CommonTree;
}
tokens {
FUNCTION_CALL;
}
equations
: equation* EOF
;
equation
: (left=expression -> expression) ('=' right=expression ->
^('=' $left $right))?
;
expression
: (t=IDENT -> IDENT) (function_call[$t] -> function_call)?
| INT
;
function_call[CommonToken t]
: '(' expression (',' expression)* ')'
-> ^(FUNCTION_CALL {$t} expression+)
;
INT : ( '0'..'9' )+ ;
IDENT : ( 'a'..'z' )+ ;
WS : (' ' | '\t' )+ { $channel = HIDDEN; } ;
NL : '\r'? '\n' { $channel = HIDDEN; } ;
Thanks, Mark
> For your grammar then you want
> to combine everything with common roots into one rule/alt and branch
> when tokens tell you that you must.
>
> Jim
>
> > -----Original Message-----
> > From: Christian Gudrian [mailto:gn at fluidon.dyndns.info]
> > Sent: Saturday, January 26, 2008 12:24 PM
> > To: antlr-interest at antlr.org
> > Subject: [antlr-interest] Problems with syntactic predicates(?)
> >
> > Hello!
> >
> > I want to write a grammar that matches equations like:
> >
> > a=1
> > 2=b
> > f_a(3)=4
> > 5=f_b(6,7)
> > f_c(8,9)
> >
> > This is my approach so far:
> >
> > // -------------------------- 8< --------------------------
> >
> > grammar test;
> >
> > INT : ( '0'..'9' )+ ;
> > IDENT : ( 'a'..'z' )+ ;
> >
> > equation
> > : ( expression '=' ) => expression '=' expression
> > | function_call
> > ;
> >
> > expression
> > : ( IDENT '(' ) => function_call
> > | INT
> > | IDENT
> > ;
> >
> > function_call
> > : IDENT '(' args? ')' ;
> >
> > args : expression ( ',' expression )* ;
> >
> > // ------------------------- >8 --------------------------
> >
> > The ANTLRWorks debugger shows me, that the equation rule matches
> > input of the form "a=1". Everything involving function calls
> > doesn't match, however; I get a NoViableAltException(0!=[null])
> > error in these cases. I suspect it's my use of syntactic
> > predicates which causes this behaviour, isn't it? What's wrong?
> >
> > Christian
> >
>
--
More information about the antlr-interest
mailing list