Problem with simple grammar [message #871408] |
Sun, 13 May 2012 12:39 |
smartmobili Missing name Messages: 5 Registered: July 2009 |
Junior Member |
|
|
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 #871410 is a reply to message #871408] |
Sun, 13 May 2012 13:44 |
|
Hi,
the problem is that your grammar is ambigous. one reason for that seems to be that you hate assignments (=,+=,?=) and (nearly) dont you them at all.
compilation_unit:
objj+=objectivej*
;
objectivej:
preprocessor_declaration
| typeDeclaration
;
preprocessor_declaration:
'@import' file=file_specification
;
file_specification:
('<'|'"')(ID ('/' | '\\' | '.')?)+ ('>' | '"')
;
typeDeclaration:
class_implementation
| category_implementation
;
class_implementation:
'@implementation'
(
name=class_name ( ':' superclass_name=superclass_name )
( instance_variables )?
)
'@end';
category_implementation:
'@implementation'
(
name=class_name '(' category_name=category_name ')'
( instance_variables=instance_variables )?
)
'@end';
class_name:
ID
;
superclass_name:
ID
;
category_name:
ID
;
instance_variables:
'{' instance_variable_declaration '}';
instance_variable_declaration:
ANY_OTHER
;
p.s: if i read the word preprocessor in conjunction with Xtext it is time to run away as fast as i can.
besides this you dont seem to care about Xtext naming conventions.
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
|
Re: Problem with simple grammar [message #871463 is a reply to message #871437] |
Sun, 13 May 2012 22:26 |
smartmobili Missing name Messages: 5 Registered: July 2009 |
Junior Member |
|
|
Ok so actually I think it's not as easy as I thought, I might need to spend years to learn this new syntax while I was just get used to antlr.
I rewrote the grammar from scratch and now I am starting with this :
Objectivej:
(types+=Type)*;
Type:
ImportDecl
//| TypeDecl
;
ImportDecl:
"@import" name=FileSpecification
;
FileSpecification:
('<'|'"')(ID ('/' | '\\' | '.')?)+ ('>' | '"')
;
But when I try it doesn't recognize the import using " but only the ones with <> :
@import <Foundation/CPObject.j> //OK
@import <AppKit/CPView.j> // OK
@import "MyClass.j" // Error : no viable input
And where can I find some real language grammar (c, java, c#, pascal, basic, ...) ?
[Updated on: Sun, 13 May 2012 22:29] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02127 seconds