java - ANTLR mismatched input expecting ID -
i still working on same haskell based grammar in case saw previous post. of errors gone have problem.
my grammar follows know large , tedious thing read need final i'm sorry , appreciate @ all: (a lot of logic missing these rules required)
grammar t; options { language=java; backtrack=true; } tokens{ num = 'num'; list = 'list'; = 'even'; odd = 'odd'; twice = 'twice'; head = 'head'; tail = 'tail'; length = 'length'; max = 'max'; rev = 'reverse'; inc = 'incall'; sort = 'sort'; keep = 'keep'; drop = 'drop'; poly = 'poly'; } @header { import java.util.hashtable; import java.util.arraylist; } @members { hashtable <string, integer> intmemory= new hashtable<string, integer>(); hashtable <string, arraylist> listmemory= new hashtable <string, arraylist>(); } prog: stat+ ; stat: numdecl ';' //declares variable containing int |listdecl ';' //declares variable containing list |numadd ';' //adds 2 integers directly or through getting int hashtable |numeven ';' //determines whether or not int or id value |numodd ';' //determines whether or not int or id value odd |numtwice ';' {system.out.println($numtwice.value);} //double values , prints |listhead ';' {system.out.println($listhead.value);} //returns list head |listtail ';' //returns list tail |listlength ';' //{system.out.println($listlength.value);} // returns list length |listconc ';' //{system.out.println($listconc.value);} //concatenates 2 lists |listmax ';' //{system.out.println($listmax.value);} //returns max of list |listreverse ';' //{system.out.println($listreverse.value);}//reverses list |listinc ';' //{system.out.println($listinc.value);} //increments each list value 1 |listsort ';' //{system.out.println($listsort.value);}//sorts list |listconst ';' //{system.out.println($listconst.value);}//constructs list 1 |bothkeep ';' //{system.out.println($bothkeep.value);}// keeps specified number of elements list |bothdrop ';' //{system.out.println($bothdrop.value);}// drops specified number of elements list |bothpoly ';' //{system.out.println($bothpoly.value);}//evaluates polynomial @ value ; numdecl: num id '=' atom {intmemory.put($id.text, new integer($atom.value));} ; listdecl @init { int count = 0; arraylist<integer> list = new arraylist<integer>(); }: list id '=' '[' a1=atom {list.add($a1.value);} (',' a2=atom {list.add($a2.value);} )* ']' {listmemory.put($id.text, list);} ; numadd returns [int value]: e=nummult {$value=$e.value;} ('+' e1=nummult {$value+=$e1.value;} | '-' e1=nummult {$value-=$e1.value;})* {system.out.println($numadd.value);} ; nummult returns [int value]: e=atom {$value=$e.value;} ('*' e1=atom {$value*=$e1.value;} | '/' e1=atom {if($e1.value !=0) $value/=$e1.value; else system.out.println ("division 0 impossible");})* ; numeven: (ws)+ atom {if ($atom.value\%2==0) system.out.println("is even.");} ; numodd: odd (ws)+ atom {if ($atom.value\%2==1) system.out.println("is odd.");} ; numtwice returns [int value]: twice (ws)+ atom {$value= 2*($atom.value); system.out.println($value);} ; listhead returns [int value]: head headtail ; headtail returns [int value]: id {$value= (integer) listmemory.get($id.text).get(0);} |'[' x=atom (',' atom )*']' {$value=$x.value;} ; listtail: tail tailtail ; tailtail @init { int count = 0; arraylist<integer> list = new arraylist<integer>(); }: id {system.out.println("[ "); (int i=1; i<listmemory.size();i++) system.out.println(listmemory.get($id.text).get(i)+","); system.out.println(" ]");} |'[' a1=atom {list.add($a1.value);} (',' a2=atom{list.add($a2.value);} )* ']' {system.out.println("[ "); (int i=1; i<list.size();i++) system.out.println(list.get (i)+","); system.out.println(" ]");} ; listlength: length lengthtail ; lengthtail returns [int value] @init { int count = 0; arraylist<integer> list = new arraylist<integer>(); }: id {$value=listmemory.get($id.text).size();} | '[' a1=atom {list.add($a1.value);} (',' a2=atom{list.add($a2.value);} )* ']' {$value=list.size();} ; listconc: id '+' '+' conctail |'[' atom(',' atom)*']' '+' '+' conctail ; conctail returns [arraylist list]: id {$list=listmemory.get($id.text);} | '[' atom(',' atom)*']' ; listmax: max maxtail ; maxtail: id | '[' atom (',' atom )*']' ; listreverse: rev revtail ; revtail: id | '[' atom (',' atom )*']' ; listinc: inc inctail ; inctail: id | '[' atom (',' atom )*']' ; listsort: sort sorttail ; sorttail: id |'[' atom (',' atom )*']' ; listconst: '[' atom consttail '|' id '<-' id ']' ; consttail: '+' atom |'-' atom |'*' atom |'/' atom ; bothkeep: keep atom keeptail ; keeptail @init { int count = 0; arraylist<integer> list = new arraylist<integer>(); }: '[' a1=atom {list.add($a1.value);} (',' a2=atom{list.add($a2.value);} )* ']' |id ; bothdrop: drop atom droptail ; droptail returns [arraylist value] @init { int count = 0; arraylist<integer> list = new arraylist<integer>(); }: '[' a1=atom {list.add($a1.value);} (',' a2=atom{list.add($a2.value);} )* ']' |id ; bothpoly: poly atom polytail ; polytail returns [arraylist value] @init { int count = 0; arraylist<integer> list = new arraylist<integer>(); }: '[' a1=atom {list.add($a1.value);} (',' a2=atom{list.add($a2.value);} )* ']' {$value=list;} |id ; atom returns [int value]: int {$value = integer.parseint ($int.text);} | id { integer v= (integer)intmemory.get($id.text); if (v!=null) {$value=v.intvalue();} else { system.err.println("undefined variable "+$id.text); } } | '(' numadd ')' {$value=$numadd.value;} ; letter: 'a'..'z'; digit: '0'..'9'; id : 'a'..'z'('0'..'9')*; int : '0'..'9'+ ; ws : (' '|'\t'|'\n'|'\r')+ {skip();} ;
i have given following input nothing on lists yet because haven't implemented methods yet:
num x=4; num y=9; x+2; 2+3; 2-3; x-2; 2*3; x*2; 2/4; x/3; x/0; x; odd 5; odd y; twice x; twice y; twice 2;
when run through command prompt following:
c:\users\kamal\desktop\antlr>java -cp .;antlr-3.2.jar org.antlr.tool t.g c:\users\kamal\desktop\antlr>javac -cp .;antlr-3.2.jar *.java c:\users\kamal\desktop\antlr>java -cp .;antlr-3.2.jar testt line 1:4 mismatched input 'x' expecting id line 2:4 mismatched input 'y' expecting id
i appreciate @ all. if no 1 can answer. lot!
the problem same in this case.
while id
rule matches input given, there might rule matches 'x'
, 'y'
values. unfortunately, haven't got antlr set here test it, letter
rule might cause problem. should try declaring digit
, letter
rules fragment
Comments
Post a Comment