[antlr-interest] About removing the "; " In the last sentence of a block
John B. Brodie
jbb at acm.org
Wed Dec 7 13:38:53 PST 2011
Greetings!
Replace your first 5 rules (e.g. program, statement_list, statement,
compound_statement, and expression_statement) with these two rules:
program : statement_list ';' EOF ;
statement_list :
( expression (';' statement_list)? )
| ( ';' statement_list )
| ( ( sen_for | block ) statement_list? )
;
(note please find attached the complete test grammar i used to test this...)
and now that i think about it, it may not handle the last semicolon of
the program properly when the last element of the program's
statement_list is a block. sigh. maybe the statement_list for program
should have the same semicolon usage as the statement_list within a
block (e.g. the very last semicolon after the second 34 in your example
should not be present)?
anyway --- hope this helps...
-jbb
On 12/07/2011 04:27 PM, roberto wrote:
> 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
>
>
>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: Test.g
Url: http://www.antlr.org/pipermail/antlr-interest/attachments/20111207/92222a6d/attachment.pl
More information about the antlr-interest
mailing list