[antlr-interest] [SPAM] Grammar of the TINY LANGUAGE error
Arin S. Rizk
arin_rizk at hotmail.co.uk
Fri Dec 3 15:38:31 PST 2010
Hi I'm using antlr works to create a compiler that accept this code
read x;
if 0<x then
fact := 1;
repeat
fact := fact * x;
x:=x-1;
until x=0;
write fact
end
I did it but i still have errors ... the program lightsup the following (onz
between ##)
read x;
if 0<x then
fact := 1;
repeat
fact := fact * ##x##;
x:=x- ##1##;
until x ##=0;##
write fact
end
I worte the following grammer
grammar HW2 ;
options {
backtrack = true;
k=1;
/* LL(1) k=1; */
}
program :
seq_statement+
;
seq_statement
:
(seq_statement ';'statement | seq_statement)
/*statement ( ';' statement )* */
;
statement
:
(read | if_statement | repeat_until | assignment_sta |
write_sta)
;
write_sta
:
'write' exp
;
assignment_sta
: ID ':=' exp ';'
;
repeat_until
:'repeat' seq_statement 'until' exp
;
read :
'read' ID ';'
;
if_statement
:
(('if' exp 'then' seq_statement 'end')|('if' exp 'then'
seq_statement 'else' seq_statement'end'))
;
exp :
simple_exp LOG_OP simple_exp | simple_exp | ID
;
simple_exp
:
simple_exp OP term |term
;
term :
term OP factor factor | factor
;
factor :
'/(' exp '/)' | NUMBER | ID
;
fragment
Operation_stat
: (NUMBER|ID) OP (NUMBER|ID)
;
OP : ('+'|'-'|'*'|'/')
;
NUMBER :
'0'..'9'+
;
LOG_OP :
('<' | '>' | '=' | '<=' | '>=' )
;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
FLOAT
: ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
| '.' ('0'..'9')+ EXPONENT?
| ('0'..'9')+ EXPONENT
;
COMMENT
: '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
| '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
;
WS : ( ' '
| '\t'
| '\r'
| '\n'
) {$channel=HIDDEN;}
;
STRING
: '\'' ( ESC_SEQ | ~('\\'|'\'') )* '\''
;
fragment
EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
fragment
HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;
fragment
ESC_SEQ
: '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
| UNICODE_ESC
| OCTAL_ESC
;
fragment
OCTAL_ESC
: '\\' ('0'..'3') ('0'..'7') ('0'..'7')
| '\\' ('0'..'7') ('0'..'7')
| '\\' ('0'..'7')
;
fragment
UNICODE_ESC
: '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
;
( I choose debug and then type the code and use the debugger to evaluate if
my grammer will work)
But still its showing error , how can I fix it ? where did I go wrong ?
More information about the antlr-interest
mailing list