[antlr-interest] About removing the "; " In the last sentence of a block

roberto rag700504 at hotmail.com
Wed Dec 7 13:27:49 PST 2011


Hello Everyone

I tried to create a simple language where I want to delete the last ';' which appears in a block. Besides having compound blocks, that do not need ';' at the end of the sentence.

This example is what I have

*{
   a=34;;;;
   for(34)
   a=59;
}
a=34;

I would like to remove the ';' which goes behind the 59. I wondered if there anyway of say when you find a TOKEN, do not consume this but continue

expression_statement
      : ';'
      | expression (';' | *'}'*)
      ;

*For example I would like that when you find the'}'generates sentence but not because it will consume the token used by the command block.
*
=============== Here is the grammar I test ====================================

grammar lteA;

program	
	:  statement_list EOF
	;
	
statement_list
	: statement+
	;

statement
	: compound_statement
	| expression_statement
	| block
	;	
	
compound_statement
	: sen_for
	;
	
expression_statement
	: ';'
	| expression (';' | '}')
	;
	
expression	
	: ID '=' (INT | STRING | FLOAT | CHAR)
	;
	
sen_for
	: 'for' '(' INT ')'
	;
block	
	: '*' '{' statement_list '}'	
	;
		
ID  :	('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
     ;

INT
	: '0'..'9'+
	| '0' 'x' HEX_DIGIT+
     ;

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 | ~('\\'|'"') )* '"'
     ;


CHAR:  '\'' ( 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
     ;

===================================================================
  best
regard Roberto


Note:
I rewrite because was scribble when convert the HTML to text



More information about the antlr-interest mailing list