[antlr-interest] Problem with grammar

Wojciech Tomasz Cichon wtcichon at googlemail.com
Mon Mar 21 03:40:48 PDT 2011


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;


More information about the antlr-interest mailing list