[antlr-interest] Problem with grammar
Matt Fowles
matt.fowles at gmail.com
Mon Mar 21 03:56:15 PDT 2011
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
>
More information about the antlr-interest
mailing list