[antlr-interest] Solving non determinism

xdecoret xdecoret at free.fr
Tue Aug 3 14:43:35 PDT 2004


Hi,

I cannot solve a non determinism problem with my grammar:

ANTLR Parser Generator   Version 2.7.3   1989-2004 jGuru.com
foo.g:72: warning:nondeterminism upon
foo.g:72:     k==1:NAME
foo.g:72:     k==2:EQUAL
foo.g:72:     k==3:NAME,STRING,VALUE
foo.g:72:     k==4:COMA,POUND
foo.g:72:     k==5:NAME,STRING,VALUE
foo.g:72:     between alt 1 and exit branch of block

I do not understand why antlr 2.7.3 complains.

I copy the .g file below (this is a parser for bibtex format). Note
that if I use the commented line for fieldValue productions rule, the
problem does not show up. 

anyother comment about the grammar is also welcome.

header {
}		
options {
    language="Cpp";
    genHashLines = false;
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
//%%%%%%        PARSER                              %%%%%%%%%%    
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

{
}
class entriesparser extends Parser;
options {
    k=5;
    buildAST = false;
    defaultErrorHandler=false;
}
{
}
parseFile
{
    string i;
    string s;
}
    :
        {
            braceStartsValue_ = false;
        }
        (entry | stringdef)* EOF
    ;
stringdef
{
    string i;
    string s;
}
    : STRINGDEF LPARENTHESIS i=id EQUAL
        {
            braceStartsValue_ = true;
        }
        s=fieldValue RPARENTHESIS
        {
            braceStartsValue_ = false;
        }
    | STRINGDEF LBRACE i=id EQUAL
        {
            braceStartsValue_ = true;
        }
        s=fieldValue RBRACE
        {
            braceStartsValue_ = false;
        }        
    ;
entry
{
    string k;
}
    : t:TYPE LBRACE k=key COMA
        {
            braceStartsValue_ = true;
        }
        fields
        {
            braceStartsValue_ = false;
        }
        RBRACE
    ;
fields
    : (field COMA)* field (COMA)?
    ;
field
{
    string i;
    string v;
}
    : i=id EQUAL v=fieldValue
    ;
fieldValue returns [std::string v]
{
    string p;
    string q;
}
    : (p=fieldValuePart POUND {v += p;})* q=fieldValuePart {v += q; }
//     : q=fieldValuePart {v += q; }
    ;
fieldValuePart returns [std::string v]
    : t0:NAME
        {
            v = t0->getText();
        }
    | t1:STRING { v = t1->getText(); }
    | t2:VALUE  { v = t2->getText(); }      
    ;
key returns [std::string v]
    : t:NAME
        {
            v = string(t->getText());
        }
    ;
id returns [std::string v]
    : t:NAME
        {
            v = string(t->getText());
        }
    ;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
//%%%%%%        LEXER                               %%%%%%%%%%    
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

{
}
class entrieslexer extends Lexer;
options {
    k=3;
    defaultErrorHandler=false;
    caseSensitive=false;
    charVocabulary='\3'..'\377';
}
{
}
POUND : '#'
    ;
LPARENTHESIS : '('
    ;
RPARENTHESIS : ')'
    ;
RBRACE : '}'
    ;
COMA : ','
    ;
EQUAL : '='
    ;
NAME
    : ('a'..'z'|'0'..'9'|'_'|'-'|'\''|':'|'.')+ 
    ;
protected
ESC
    : '\\' ~('\n')
    ;
protected
STRING_INTERNAL
    : ( ('\\' ~('\n'))=> ESC
        | ( '\r' { newline(); }
            | '\n' { newline(); }
            | '\\' '\n'   { newline(); }
            )
        | ~( '"' | '\r' | '\n' | '\\' )
        )*
    ;
STRING: '"' t:STRING_INTERNAL '"'
        {
            $setText(t->getText());
        }
    ;
protected
LBRACE : '{'
    ;
protected
VALUE_INTERNAL
    : ((~('{'|'}'|'\n')) | '\n' { newline(); } | VALUE)*
    ;
protected
VALUE
    : ('{' VALUE_INTERNAL '}')
    ;
LBRACE_OR_VALUE
    : { parser_->braceStartsValue() }? t:VALUE
        {
            $setType(VALUE);
            string s = t->getText().substr(1,t->getText().size()-2);
            $setText(s);
        }
    | LBRACE
        {
            $setType(LBRACE);
        }
;
// The \r\n below is to parse DOS file end of lines
WS
    : ( ' ' | '\t' | ('\n'| "\r\n") { newline(); })
        {
            $setType(ANTLR_USE_NAMESPACE(antlr)Token::SKIP);
        }
	;
protected
TYPE_INTERNAL
    : ('a'..'z')+
    ;
protected
TYPE
    : '@' t:TYPE_INTERNAL
        {
            $setText(t->getText());
        }
    ;
protected
STRINGDEF
    : "@string"
    ;

STRINGDEF_OR_TYPE
    : ("@string") => STRINGDEF
        {
            $setType(STRINGDEF);
        }
    | ('@') => t:TYPE
        {
            $setType(TYPE);
            $setText(t->getText());
        }
    ;





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list