[antlr-interest] Problem with grammar

Jim Idle jimi at temporal-wave.com
Mon Mar 21 08:29:11 PDT 2011


The nested ^ are fine but without a subrule, the node will not have start
and stop indexes correctly updated. The main problem with this grammar is
that it has no structure and it looks like it is ambiguous. First remove
the 'literals' and make real tokens, then start with one of the simpler
examples that has an expression tree and model your grammar on that.

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Matt Fowles
> Sent: Monday, March 21, 2011 3:56 AM
> To: Wojciech Tomasz Cichon
> Cc: ANTLR LIST
> Subject: Re: [antlr-interest] Problem with grammar
>
> Wojciech~
>
> Not sure what the issue is; however, I might suggest that the rule
>
> function_call : ident '('  (ident_arg ( ident_arg2 )* )? ')' SEMI ->
> ^(CALL ident ^(PARAMS (ident_arg (  ident_arg2 )* )?));
>
> is better written
>
> function_call : function_ref '('  (arg (',' arg)*)? ')' SEMI -> ^(CALL
> function_ref ^(PARAMS arg*)); function_ref : ident; arg : ident;
>
>
> I am also not 100% sure that the nested '^' on the rhs of the rewrite
> are legal.  Our grammar does function calls more like:
>
> function_call : function_ref '('  (arg (',' arg)*)? ')' -> ^(CALL
> function_ref arg*); function_ref : ident; arg : ident;
>
> Matt
>
> On Mon, Mar 21, 2011 at 10:40 AM, Wojciech Tomasz Cichon
> <wtcichon at googlemail.com> wrote:
> > hi,
> > i have prioblem with one rule from my grammar factor ...
> > | ident '('( ident_arg (ident_arg2)*)? ')' á-> á^(CALL ident ^(PARAMS
> > | (ident_arg ( áident_arg2 )* )?))
> >
> > after i send line :
> > c = 2*a+ f(1,4);
> > i received error:
> > line 17:11 mismatched input '1' expecting ')'
> > it looks like itĺs completely ignores possibility of having any
> > arguments can someboduy tell me what iĺm doing wrong here and how i
> > can fix that regards
> >
> >
> > my grammar looks like that
> > grammar SmallC;
> >
> > options {
> > álanguage = Java;
> > áoutput = AST;
> > // backtrack = true;
> > // memoize = true;
> > á ák á= 3;
> > }
> >
> > tokens
> > {
> > CALL;
> > SET;
> >
> > IF;
> > ELSE;
> > WHILE;
> >
> > READ;
> > OUT;
> > PRINT;
> > RETURN;
> > READC;
> > OUTC;
> >
> > BODY;
> > DECLS;
> > MAIN;
> > PROCEDURE;
> > ARGS;
> > INCLUDE;
> > PROGRAM;
> > PARAMS;
> > }
> > program: áincludes decls procedure* main;
> >
> > includes : ('#include' string)* -> ^(INCLUDE string*);
> >
> > main : 'main' '(' ')' body á-> ^(MAIN body) ;
> >
> > procedure : TYPE ident '(' args ')' body -> ^(PROCEDURE ident TYPE
> > args body);
> >
> > args : (typedident (',' restargs)*)? á-> ^(ARGS á(typedident
> > restargs*)?);
> >
> > restargs : typedident;
> >
> > body : '{' decls stmtlist '}' -> á ^(BODY decls stmtlist);
> >
> > decls : (typedident SEMI)* -> ^(DECLS typedident*);
> >
> > typedident: TYPE^ ident;
> >
> > TYPE : 'int' |'char';
> >
> > stmtlist : stmt*;
> >
> > stmt á: '{' stmtlist '}' -> á stmtlist
> > á á á| 'while' '(' áexp ')' ástmt -> ^(WHILE exp stmt)
> > á á á|'if' '(' áexp ')' stmt -> ^(IF exp stmt)
> > á á á| ident '=' lexp SEMI á-> ^(SET ident lexp)
> > á á á| 'read' '(' ident ')' SEMI -> ^(READ ident)
> > á á á| 'output' '(' ident ')' SEMI -> ^(OUT ident)
> > á á á| 'print' '(' string ')' SEMI á-> á^(PRINT string )
> > á á á| 'return' lexp? SEMI á-> ^(RETURN lexp?)
> > á á á| 'readc' '(' ident ')' SEMI -> ^(READC ident)
> > á á á| 'outputc' '(' ident ')' SEMI -> ^(OUTC ident)
> > á á á| ident '(' á(ident_arg ( ident_arg2 )* )? ')' SEMI -> ^(CALL
> > ident ^(PARAMS (ident_arg ( áident_arg2 )* )?))
> >
> > á á ;
> > exp : lexp (COMP^ rexp)?;
> >
> >
> > rexp : lexp;
> >
> >
> >
> > lexp : term (SIMOP^ álexp)?;
> >
> >
> >
> > term á: factor (OP^ áterm)?;
> >
> >
> >
> > factor á:
> > á á á//'-'?
> > á á á(NUMBER |ident )^
> > á á á á| '(' exp á')' -> exp
> > á á á á| character
> > á á á á| ident '('( ident_arg (ident_arg2)*)? ')' á-> á^(CALL ident
> > ^(PARAMS (ident_arg ( áident_arg2 )* )?))
> >
> > á á á ;
> > //(typedident (',' restargs)*)?
> >
> > á áident_arg á: á á áident;
> > á áident_arg2 : ',' ident;
> >
> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > Unsubscribe:
> > http://www.antlr.org/mailman/options/antlr-interest/your-email-
> address
> >
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address


More information about the antlr-interest mailing list