[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Head required (LONG)




When I try to run the test it ends with:

(1,3): Head expected.

But there IS a head token there (GRAMMARTYPE) that suíts the rule! Whats
wrong here?


You have to be precise about the parse error message, it's:

[3,1] expecting: head


The reason why this error is returned during parsing, is situated in your token definitions:

char = ''' not_cr_lf ''';
id = id_part ('_' id_part)*;
string = '"' [not_cr_lf - '"']+ '"';
token = uppercase (uppercase | digit | '_')*;
head = uppercase (uppercase | digit | '_')*;

You have to know that, for a given input, the longest matching token will be returned by the lexer. In the case of two matches of the same length, the token listed first will be returned. Now, as your "token"-token and "head"-token are syntactically exactly the same (i.e. the right-hand side of their definition is the same), the lexer will always return the "token"-token because it is listed first, and that's why the parser-error given above is returned because the parser expects a "head"-token while the lexer returns a "token"-token.

Such token-problems do not only appear when the right-hand side of the definitions of two tokens are exactly the same, but also, for example you defined only two tokens:

name_one = uppercase lowercase*;
name_two = uppercase uppercase*;


For the given input:

A

the lexer will always return the "name_one"-token because it is listed first, although some defined production rule would expect the "name_two"-token.

Next to that, make sure that your token defintions of your keywords are listed first. For obvious reasons, following "keyword"-token will never be returned by the lexer:

name = lowercase+;
keyword = "define"


because the specified keyword is also syntactically a legal name.


Hope this helps,
Pieter.