Files

40 lines
956 B
Plaintext

%parser Program 4.8
%{
public class ex {
public static void Main(string[] args) {
Parser p = new syntax(new tokens());
p.Parse(new StreamReader(args[0]));
}
public static Hashtable dict = new HashTable();
public static int get(ID id) { return (int)dict[id.yytext]; }
public static void put(ID id,int v) { dict[id.yytext]=v; }
}
%}
%right SEMICOLON COMMA
%left PLUS MINUS
%left TIMES DIV
prog : stm:s ;
stm : stm:a SEMICOLON stm:b ;
stm : ID:id ASSIGN exp:e { ex.put(id,e); };
stm : PRINT LPAREN exps:e RPAREN { Console.WriteLine(); };
exps : exp:e { Console.Write(e); };
exps : exps COMMA exp;
exp : INT:i { $$ = i.val; };
exp : ID:id { $$ = ex.get(id); } ;
exp : exp:a PLUS exp:b { $$ = a + b; };
exp : exp:a MINUS exp:b { $$ = a - b; };
exp : exp:a TIMES exp:b { $$ = a * b; };
exp : exp:a DIV exp:b { $$ = a / b; };
exp : stm:s COMMA exp:e { $$ = e; };
exp : LPAREN exp:e RPAREN { $$ = e; };