dcsimg
Help with v3 grammar
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Marcus_Edwards
Posted On:   Tuesday, March 13, 2007 04:28 AM

I am very new to ANTLR and although I've followed the examples and tutorials I can't get the following very simple (I thought!) grammar for a parsing a template language working. I am using AntlrWorks to develop and debug out the grammar. --8<-- grammar jstemplate; TEMPLATE : ( MACRO | text=TEXT { System.out.println("TEXT: " + text); } )* EOF; fragment MACRO : (IF (TEMPLATE)* (ELSE (TEMPLATE)*)? END) |(VAR) ; fragment TEXT : (~'{')+ ; fragment PATH : ('A'..'Z' | 'a'..'z' | '0'..'9' | '-' | '_' | '/' | '[' | ']' | '=' | '@' | '.' |'\''|'"')+ ; fragment IF : '{if' WS+ path   More>>

I am very new to ANTLR and although I've followed the examples and tutorials I can't get the following very simple (I thought!) grammar for a parsing a template language working. I am using AntlrWorks to develop and debug out the grammar.



--8<--


			
grammar jstemplate;

TEMPLATE
: ( MACRO
| text=TEXT { System.out.println("TEXT: " + text); }
)*
EOF;

fragment MACRO
:
(IF (TEMPLATE)* (ELSE (TEMPLATE)*)? END)
|(VAR)
;
fragment TEXT
:
(~'{')+
;
fragment PATH
: ('A'..'Z' | 'a'..'z' | '0'..'9' | '-' | '_' | '/' | '[' | ']' | '=' | '@' | '.' |'\''|'"')+
;
fragment IF : '{if' WS+ path=PATH '}' { System.out.println("IF " + path); }
;

fragment ELSE : '{else}'
{ System.out.println("ELSE"); }
;

fragment END : '{end}'
;
fragment VAR : '{var' WS+ path=PATH '}' { System.out.println("VAR " + path); }
;
fragment WS
: ' ' | ' '
;

--8<--



When I check the grammar I get the following warning:



11:36: Decision can match input such as "'u0000'..'uFFFE'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input



If I try and generate code from this grammar I get
syntax error: codegen: :0:0: unexpected end of subtree.



The following is the test input



--8<--


			
This is a simple test of {if foo/bar}bar{else}baz{end}. Including {var foo/foo}.

--8<--



Any ideas on how I can fix this grammar?

   <<Less
About | Sitemap | Contact