Problem with simple grammar [message #871408] |
Sun, 13 May 2012 08:39  |
Eclipse User |
|
|
|
Hi,
I am trying to add support for a web language called ObjectiveJ and I am starting with the following grammar :
compilation_unit:
objj+=objectivej*
;
objectivej:
preprocessor_declaration
| typeDeclaration*
;
preprocessor_declaration:
'@import' file_specification
;
file_specification:
('<'|'"')(ID ('/' | '\\' | '.')?)+ ('>' | '"')
;
typeDeclaration:
class_implementation
| category_implementation
;
class_implementation:
'@implementation'
(
class_name ( ':' superclass_name )
( instance_variables )?
)
'@end';
category_implementation:
'@implementation'
(
class_name '(' category_name ')'
( instance_variables )?
)
'@end';
class_name:
ID
;
superclass_name:
ID
;
category_name:
ID
;
instance_variables:
'{' instance_variable_declaration '}';
instance_variable_declaration:
ANY_OTHER
;
My goal is first to be able to parse the following code :
@import <Foundation/CPObject.j>
@import <AppKit/CPView.j>
@import "MyClass.j"
@implementation Address : CPObject
{
CPString name;
CPString city;
}
// a line comment
@end
But when I try to generate the parser I get some errors like :
warning(200): ../org.cappuccino.lang/src-gen/org/cappuccino/lang/parser/antlr/internal/InternalObjectiveJ.g:98:1: Decision can match input such as "{EOF, '@import', '@implementation'}" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
error(201): ../org.cappuccino.lang/src-gen/org/cappuccino/lang/parser/antlr/internal/InternalObjectiveJ.g:98:1: The following alternatives can never be matched: 2
warning(200): ../org.cappuccino.lang/src-gen/org/cappuccino/lang/parser/antlr/internal/InternalObjectiveJ.g:142:1: Decision can match input such as "'@implementation'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): ../org.cappuccino.lang/src-gen/org/cappuccino/lang/parser/antlr/internal/InternalObjectiveJ.g:119:1: Decision can match input such as "'@import'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
What am I doing wrong in my grammar ?
Thanks
|
|
|
|
|
|
|
|
|
Re: Problem with simple grammar [message #915888 is a reply to message #871467] |
Tue, 18 September 2012 02:52  |
Eclipse User |
|
|
|
The terminals are to come from lexer which is normally tightly coupled with parser. So parser is a simple crawler on a state-machine (FA). SM is created by parser generators (see the tables generated by yacc for a grammar). For an SR parser, during the S (shift) the parser wants a token and that is provided by lexer. So parser does not know how to read the file or tokenize - it is the job of lexer and pre-processor (if applicable for the language) to supply the terminal tokens (in the form of an integer - for eg IDENTIFIER means 10, while means 11 ...) during the shift operation of the crawler. Normally the lex spec files ends with .l where you have to specify the regular grammar for your lexer so that the file is tokenized as per the lexical specs of the language in question.
Crawler gets into issues, when conflicts happen as crawler simply goes by the state tables (normally there would be 3 tables, shift-states, reduce states, reduce-maps - if my memory is right !). So the parser generator has to produce the tables in the right way for the language in question. For that you have to have unambiguous grammar. Normally SR conflict is resolved by taking the S path, RR is resolved by the 1st production of the grammar among the applicable productions for top of the state-stack.
The theory is simple discrete mathematics. If you have interest you can learn much faster.
Regards
Veda
|
|
|
Powered by
FUDForum. Page generated in 0.04149 seconds