[antlr-interest] (no subject)

Harrie Hoenjet hoenjethc at gmail.com
Fri Mar 9 03:13:04 PST 2012


Hi,

I build the following grammar in ANTL3; I live in the Netherlands.

See below

 When I run the grammar with the following text

 program Test {

 //declaraties

 int i = -5; // use of unaire min

int j; //

int k = -i; // k = 5

 //assignments

i = i + 1; // i = 4

j = 2 * i + k; //j = -3

 // expected values : i = -4, j = -3, k = 5

}

 I get the following error message

Prog0.txt

Factor : -5

Term: -5

Undefined variabl1 i

 Who can help me?

 Thanks in advance,

Harrie

 grammar SimpleLR;

 tokens {

PLUS = '+' ;

MINUS = '-' ;

MULT = '*' ;

DIV = '/' ;

IS = '=';

PUNTKOMMA = ';';

ACCOPEN = '{';

ACCLOSE = '}';

HAAKJEL = '(';

HAAKJER = ')';

KOMMA = ',';

GT = '>';

}

 @header {

import java.io.*;

import java.util.HashMap;

}

 @members {

 // Map variabele name to Integer object holding value

public static HashMap memory = new HashMap();

 public static void main(String[] args) throws Exception {

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

SimpleLRLexer lex = new SimpleLRLexer(new ANTLRFileStream(in.readLine()));

CommonTokenStream tokens = new CommonTokenStream(lex);

SimpleLRParser parser = new SimpleLRParser(tokens);

 try {

parser.prog();

System.out.println("OK");

} catch (RecognitionException e) {

e.printStackTrace();

}

}

}

 /*------------------------------------------------------------------

* PARSER RULES

*------------------------------------------------------------------*/

 prog

: PROG UNAME ACCOPEN cmds ACCLOSE;

 cmds

: (cmd)+;

 cmd

: decl | keuze | opdracht;

 decl

: VAR LNAME (IS expr)? PUNTKOMMA;

 keuze

: IF HAAKJEL conditie HAAKJER

(ACCOPEN opdracht* ACCLOSE)* PUNTKOMMA

(ELSE ACCOPEN opdracht ACCLOSE)? PUNTKOMMA;

 conditie

: opdracht GT expr;

 opdracht

: lwaarde IS expr {memory.put($lwaarde.text, new Integer($expr.value));

System.out.println("expressie: " + memory);} PUNTKOMMA;

 lwaarde

: LNAME;

 expr returns [int value]

: t=term {$value = $t.value;}

(

PLUS t=term {$value += $t.value;}

|

MINUS t=term {$value -= $t.value;}

)*

;

 term returns [int value]

: f=factor {$value = $f.value;}

(

MULT f=factor {$value = $value * $f.value;}

|

DIV f=factor {$value = $value / $f.value;}

)*

{System.out.println("term: " + $value);}

;

 factor returns [int value = 1]

: (MINUS {$value = -$value;})?

(NUMBER {$value = $value *Integer.parseInt($NUMBER.text);}

|LNAME {Integer iv = (Integer)memory.get ($LNAME.text);

if (iv!= null) $value = $value *(iv.intValue());

else System.out.println("undefined variable "+ $LNAME.text);

}

)

{System.out.println("factor: " + $value);}

;

 /*------------------------------------------------------------------

* LEXER RULES

*------------------------------------------------------------------*/

 PROG : 'program';

VAR : 'int';

IF : 'if';

ELSE : 'else';

UNAME : UPPER(LOWER|UPPER)*;

 LNAME : LOWER(LOWER|UPPER)*;

NUMBER : (DIGIT)+ ;

 WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { $channel = HIDDEN;
} ;

 LINE_COMMENT : '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;};

 fragment DIGIT : '0'..'9' ;

 fragment LOWER : ('a'.. 'z' );

 fragment UPPER : ('A' .. 'Z');


More information about the antlr-interest mailing list