prog ::= [ decl ; ]* stmt EOF
decl ::= var ID [ , ID ]* : type
type ::= integer | boolean
expr ::= andExpr [ or andExpr ]*
andExpr ::= relExpr [ and relExpr ]*
relExpr ::= addExpr [ = addExpr | != addExpr
| <= addExpr | >= addExpr
| < addExpr | > addExpr ]?
addExpr ::= mulExpr [ + mulExpr | - mulExpr ]*
mulExpr ::= unExpr [ * unExpr | / unExpr ]*
unExpr ::= + unExpr | - unExpr | not unExpr | primary
primary ::= ( expr ) | ID | NUM | true | false
stmt ::= ID := expr
| print( expr )
| if( expr ) then stmt [ else stmt]*
| while( expr ) do stmt
| begin stmt [ ; stmt ]* end
@members {
SymbolTable st = new SymbolTable();
}
public enum Type { INTEGER, BOOLEAN }
prog
: ( decl ';')* stmt EOF
;
decl
: 'var' i1 = ID ( ',' i2 = ID )* ':' type
{
/* CODE... */
}
;
type
: 'integer' | 'boolean'
;
expr returns [Type type]
: e1 = andExpr
( 'or' e2 = andExpr
{ if(($e1.type || $e2.type) != Type.BOOLEAN)
throw new IllegalArgumentException("Type error in OR ");
$type = Type.BOOLEAN;
}
)*
;
andExpr returns [Type type]
: e1 = relExpr
( 'and' e2 = relExpr
{ if(($e1.type || $e2.type) != Type.BOOLEAN)
throw new IllegalArgumentException("Type error in AND ");
$type = Type.BOOLEAN;
}
)*
;
relExpr returns [Type type]
: e1 = addExpr { $type = $e1.type; }
( '=' e2 = addExpr
{ if($e1.type != $e2.type)
throw new IllegalArgumentException("Type error in = ");
$type = Type.BOOLEAN;
}
| '<''>' e3 = addExpr
{ if($e1.type != $e3.type)
throw new IllegalArgumentException("Type error in <> ");
$type = Type.BOOLEAN;
}
| '<''=' e4 = addExpr
{ if(($e1.type || $e4.type) != Type.INTEGER)
throw new IllegalArgumentException("Type error in <= ");
$type = Type.BOOLEAN;
}
| '>''=' e5 = addExpr
{ if(($e1.type || $e5.type) != Type.INTEGER)
throw new IllegalArgumentException("Type error in >= ");
$type = Type.BOOLEAN;
}
| '<' e6 = addExpr
{ if(($e1.type || $e6.type) != Type.INTEGER)
throw new IllegalArgumentException("Type error in < ");
$type = Type.BOOLEAN;
}
| '>' e7 = addExpr
{ if(($e1.type || $e7.type) != Type.INTEGER)
throw new IllegalArgumentException("Type error in > ");
$type = Type.BOOLEAN;
}
)?
;
addExpr returns [Type type]
: e1 = mulExpr { $type = $e1.type; }
( '+' e2 = mulExpr
{ if(($e1.type || $e2.type) != Type.INTEGER)
throw new IllegalArgumentException("Type error in + ");
$type = Type.INTEGER;
}
| '-' e3 = mulExpr
{ if(($e1.type || $e3.type) != Type.INTEGER)
throw new IllegalArgumentException("Type error in - ");
$type = Type.INTEGER;
}
)*
;
mulExpr returns [Type type]
: e1 = unExpr { $type = $e1.type; }
( '*' e2 = unExpr
{ if(($e1.type || $e2.type) != Type.INTEGER)
throw new IllegalArgumentException("Type error in * ");
$type = Type.INTEGER;
}
| '/' e3 = unExpr
{ if(($e1.type || $e3.type) != Type.INTEGER)
throw new IllegalArgumentException("Type error in / ");
$type = Type.INTEGER;
}
)*
;
unExpr returns [Type type]
: '+' e1 = unExpr
{ if($e1.type != Type.INTEGER)
throw new IllegalArgumentException("Type error in + ");
$type = Type.INTEGER;
}
| '-' e2 = unExpr
{ if($e2.type != Type.INTEGER)
throw new IllegalArgumentException("Type error in - ");
$type = Type.INTEGER;
}
| 'not' e3 = unExpr
{ if($e3.type != Type.BOOLEAN)
throw new IllegalArgumentException("Type error in not ");
$type = Type.BOOLEAN;
}
| primary { $type = $primary.type; }
;
primary returns [Type type]
: '(' expr ')' { $type = $expr.type; }
| ID { $type = st.lookupType($ID.text); }
| NUM { $type = Type.INTEGER; }
| 'true' { $type = Type.BOOLEAN; }
| 'false' { $type = Type.BOOLEAN; }
;
stmt
: ID ':''=' expr
{ if(st.lookupType($ID.text) != $expr.type)
throw new IllegalArgumentException (" errore di tipo ");
}
| 'print' '(' expr ')' { $type = $expr.type; }
| 'if' expr 'then' s1 = stmt ( 'else' s2 = stmt )?
{ if(expr != Type.BOOLEAN)
throw new IllegalArgumentException("Type error in ( expr ) ");
}
| 'while' expr 'do' s1 = stmt
{ if(expr != Type.BOOLEAN)
throw new IllegalArgumentException("Type error in ( expr ) ");
}
| 'begin' s1 = stmt ( ';' s2 = stmt )* 'end'
;
ID : ('a'.. 'z'|'A'.. 'Z') ('a'.. 'z'|'A'.. 'Z'|'0'.. '9'|'_')* ;
NUM : '0'.. '9'+ ;
WS : (' ' | '\t' | '\r' | '\n')+ { skip(); } ;
import java.util.*;
public class SymbolTable {
private Map <String, Type> typeMap = new HashMap <String, Type>();
public void insert(String s, Type t) {
if(!typeMap.containsKey(s))
typeMap.put(s, t);
else
throw new IllegalArgumentException("Error " + s);
}
public Type lookupType(String s) {
if(typeMap.containsKey(s))
return typeMap.get(s);
else
throw new IllegalArgumentException("Error " + s);
}
}
--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
until (there are no occurrences)
enter the value in the symbol table ID and Type
...
$type = Type<span style="color
Thanks for the reply but I don't understand.. Sorry..
What do you mean with 'a list of StmtContext'?
...
$type = Type<span style="color
--
if((e1 || e3) != Type.INTEGER)
throw new IllegalArgumentException("Type error in - ");
type = Type.INTEGER;
}
@members { SymbolTable st = new SymbolTable();}
prog : ( decl ';')* stmt EOF ; decl : 'var' i1 = ID ( ',' i2 = ID )* ':' type
{ String[] sing; for(el = split(" , | : ") && el != null) { el = split(" , | : ");
{ if(st.lookupType($ID.text) != st.lookupType($expr.text)) throw new IllegalArgumentException ("Errore di tipo."); } | 'print' '(' expr ')'
| 'if' expr 'then' s1 = stmt ( 'else' s2 = stmt )? { if(expr != Type.BOOLEAN) throw new IllegalArgumentException("Type error in ( expr ) "); } | 'while' expr 'do' s1 = stmt { if(expr != Type.BOOLEAN) throw new IllegalArgumentException("Type error in ( expr ) "); } | 'begin' s1 = stmt ( ';' s2 = stmt )* 'end' ; ID : ('a'.. 'z'|'A'.. 'Z') ('a'.. 'z'|'A'.. 'Z'|'0'.. '9'|'_')* ;
NUM : '0'.. '9'+ ;
WS : (' ' | '\t' | '\r' | '\n')+ { skip(); } ;
...
$type = Type<span style="color
...
$type = Type<span style="color
...
$type = Type<span style="color
...
$type = Type<span style="color
I’m sure there are several people on this list that know multiple ways to address this issue. The problem is you asked a question which requires significant time to answer fully, and that time has real opportunity costs associated with it.
Personally, the presentation of the problem appeared to be the transcription of a homework problem plus a minimal amount of work attempting to solve it. The missing part for me is I didn’t feel like the time I put into creating a solution would be truly appreciated by anyone. It might end up as part of a good grade on some assignment, but I already know I could get a great grade on it.
If you can explain, in detail, how an answer to this problem would benefit the larger community, then I’ll put time into helping you with it. Just remember that I’ll be helping you instead of spending my weekend time with my family and/or working on research problems (e.g. issues #490 and #522) that will hopefully lead to me graduating.
Sam
--