<html>
<body>
The first version is failing on the <i>associative_dimension_1</i> not
having a <i>star</i> or <i>data_type</i> and no other alternative
available.<br><br>
The second version falls through the <i>associative_dimension_2</i> to
<i>sized_or_unsized_dimension</i> to recognize the '[]' <br><br>
Note, both have a bit of backtracking that may be problematic on more
complex inputs.<br><br>
HTHs,<br>
Gerald<br><br>
At 10:22 AM 11/3/2009, alex.marin@amiq.ro wrote:<br>
<blockquote type=cite class=cite cite="">Hello,<br><br>
We have trouble understanding why we get a "no viable
alternative" when<br>
running the attached parser grammar on the following input:<br><br>
bit bitstream [];<br><br>
The output is:<br><br>
line 1:15 no viable alternative at input ']'<br><br>
However, we have found two (very strange) workarounds for the issue:<br>
1. Commenting out the 'real' option in the data_type rule<br>
2. Using associative_dimension_2 rule instead of
associative_dimension_1<br>
(although the two are equivalent)<br><br>
What is the explanation for this behavior?<br>
Is there a rigurous solution to avoid such behavior?<br><br>
Thanks,<br>
Alex Marin<br><br>
Notes:<br>
- the example is not intended to be useful by itself (it is an excerpt
from<br>
a much larger grammar)<br>
- the latest antlr version has been used for code generation<br>
(antlr-3.2.jar)<br>
- you can find the referred grammar inline at the end of this e-mail
and<br>
also in the attached file<br>
- by comparing the generated parsers, we noticed that the workarounds
cause<br>
the prediction to be done by some complicated if-conditions rather than
the<br>
dfa which throws the NoViableAlt<br><br>
////////////////// Example.g ////////////////////////<br><br>
grammar Example;<br><br>
options {<br>
<x-tab> </x-tab>k=1;<br>
<x-tab> </x-tab>
output=AST;<br>
<x-tab> </x-tab>}<br><br>
entry<br>
:<br>
(my_rule)+<br>
;<br><br>
my_rule<br>
<x-tab> </x-tab>:<br>
<x-tab> </x-tab>
tf_port_item SEMI<br>
<x-tab> </x-tab>;<br><br>
tf_port_item<br>
: <br>
data_type ID variable_dimension<br>
;<br><br>
<br>
data_type<br>
:<br>
'bit'<br>
| 'byte'<br>
| 'real' // Comment this to suppress NoViableAlt<br>
| 'struct' <br>
| 'union' ( 'tagged' )? <br>
| 'enum' <br>
| 'virtual'<br>
| ps_identifier <br>
;<br><br>
ps_identifier<br>
:<br>
( ID COLON_COLON ) => ID COLON_COLON ID <br>
| ID<br>
;<br>
<x-tab> </x-tab><br>
variable_dimension<br>
:<br>
( associative_dimension_1 ) => associative_dimension_1
variable_dimension <br>
// comment this line<br>
// ( associative_dimension_2 )
=> associative_dimension_2<br>
variable_dimension // and uncomment this one to suppress
NoViableAlt (<br>
with 'real' alt in data_type)<br>
| ( sized_or_unsized_dimension )*<br>
;<br><br>
associative_dimension_1<br>
:<br>
LBRACK ( STAR | data_type ) RBRACK<br>
; <br><br>
associative_dimension_2<br>
<x-tab> </x-tab>
:<br>
( LBRACK STAR ) => LBRACK STAR RBRACK<br>
| LBRACK data_type RBRACK<br>
<x-tab> </x-tab>
;<br><br>
<br>
sized_or_unsized_dimension <br>
:<br>
LBRACK ( NUMBER )? RBRACK <br>
;<br><br>
/********** Lexer *************/<br><br>
SEMI: ';';<br>
STAR: '*';<br>
LBRACK: '[';<br>
RBRACK: ']';<br>
COLON_COLON: '::';<br><br>
WS<br>
:<br>
(' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}<br>
;<br><br>
ID<br>
:<br>
('a'..'z'|'A'..'Z'|'_') ('0'..'9'|'a'..'z'|'A'..'Z'|'_')*<br>
;<br><br>
NUMBER<br>
:<br>
('0'..'9')+<br>
;<br><br>
<br>
Content-Type: application/octet-stream; charset="UTF-8";<br>
name="Example.g"; <br>
Content-Disposition: attachment;<br>
filename="Example.g"; <br><br>
<br>
List:
<a href="http://www.antlr.org/mailman/listinfo/antlr-interest" eudora="autourl">
http://www.antlr.org/mailman/listinfo/antlr-interest</a><br>
Unsubscribe:
<a href="http://www.antlr.org/mailman/options/antlr-interest/your-email-address" eudora="autourl">
http://www.antlr.org/mailman/options/antlr-interest/your-email-address</a>
</blockquote></body>
</html>