0

public class FirstExample { // 在这个例子中,我们在类中定义了一些属性

WRITE // This WRITE command follows a rule type defined in my grammar
Hello World
private
String firstTitle ;
private
String secondTitle ;
private
Integer firstAmount ;
private
Integer secondAmount ;
SET
firstTitle = "Ramesh"
SET
secondTitle = "Suresh"
SET
firstAmount = 100
SET
secondAmount = 200 

当我将命令“ WRITE ”更改为“ WRITEE ”时,每行都会突出显示错误。在 Eclipse Java 编辑器中,当我们编写错误的语法时,错误只会显示在该行上。过去 2 天我一直在寻找此问题的解决方案,但找不到解决方案。请帮助我。由于发布问题时出现了10 条声誉消息,我无法上传图片。

我正在发送语法。请检查一次。

grammar org.xtext.example.mydsl.MyPoc with org.eclipse.xtext.common.Terminals  hidden (GUESS_COMMENT,WS)

generate myPoc "http://www.xtext.org/example/mydsl/MyPoc"

Domainmodel:
    (elements+=Type)*;

Type:Class;

// This is the starting point of the grammar
 Class:
    (packageList+=packageList)*
    (packageList+=Import)*
    (directives+=Directives) 'class' name=ID ('extends' superType=[Class])? '{'
    (greetings+=Greeting)*
    (features+=Feature)*
    setValues+=SetValues*
    operations+=Operation*
    functionCall+=ArithmeticOperation*
    directives+=Directive*
    allMethod+=AllMethod*
    allMethodInClass+=AllMethodInClass*
    samples+=Samples*
    '}'; 

//Package list consist of all the packages we want to include in our class  
 packageList:
    ('package' name=ID) ('.'?) (classes+=[Class])?;

//Import list consist of all the packages we want to include in our class  
 Import:
    'import' importedNamespace=QualifiedNameWithWildcard;

QualifiedNameWithWildcard:
    'QualifiedName' '.*'?;

Greeting: 
(directives+=Directives)'Hello World'';'
(directives+=Directives)?'bye'?';'
(directives+=Directives)?';'
;

// Features defines the properties of that class 
// In our grammar by default we make the property private
// our grammar takes two types of DataType String or Integer 
// Name of the property given as per rule ID defined in Terminal file
Feature:
    (directives+=Directives)* type=DATA_TYPE name=ID ';';

    // In SetValues we initialize the properties by using SET directive
SetValues:
    (directives+=Directives) name=ID ('='?) (INT | STRING)';';

    //In Operation we define the function,arguments with there Data Type  
Operation:
    'Do' name=ID ('(' types+=DATA_TYPE args+=ID (',' types+=DATA_TYPE
    args+=ID)* ')')?';';

    //In Arithmetic Operation we call the function using $ passing the parameters
ArithmeticOperation:
    (directives+=Directives)? name=ID '=' (directives+=Directives)? ('$')? types+=[Operation] ('(' (args+=ID) ','
    (args+=ID)* ')')?';';

    //In Samples we use Do While loop and inside that loop we use if else block     
Samples:
    name=ID (directives+=Directives) ('!,' STRING)? loopConstruct+=LoopConstruct* operations+=Operation*;

    // In AllMethod we can call any method by using class name with '.' operator  
AllMethod:
    (classes+=[Class]) "." types+=[Operation] ('(' (args+=INT | ID) ',' (args+=INT | ID)* ');')?;

    // In AllMethodInClass we can see all methods present in that class
AllMethodInClass:
    types+=[Operation] ('(' (args+=INT | ID) ',' (args+=INT | ID)* ');')?;

//In LoopConstruct we define how we can use Do while loop with directive types given below
LoopConstruct:
    (directives+=Directives) ("{"
    (directives+=Directives ('!,' STRING))*
    (directives+=Directive)*
    "}")
    (directives+=Directives) ("{"
    (directives+=Directives ('!,' STRING))*
    (directives+=Directive)*
    "}")?
    (directives+=Directives)?;

Directive:
    (directives+=Directives) ('(' name=ID LOOP_CODITION_CHECK name=ID ')')?
    '{' (directives+=Directives ('!,' STRING)?)* '}';

Directives:
    name=DirectiveType;

DirectiveType:
    {DirectiveType} value=DIRECTIVES_TYPE;

    // From here all are rules which shows that what we can use in our grammar 
terminal DATA_TYPE:
    ('Integer' | 'String');

// These are some directives
// WRITE is following this rule
terminal DIRECTIVES_TYPE:
    ('SET' | 'WRITE' | 'READ' | 'QUIT' | 'If' | 'ElseIf' | 'Else' | 'EndIf' | 'Do' | 'while' | 'private' | 'public' | 'do');

terminal LOOP_CODITION_CHECK:
    ('=' | '>' | '<' | '!')+;

terminal GUESS_COMMENT : '//' !('\n'|'\r')* ('\r'? '\n');

我想明确指出,WRITE 不仅是问题所在。语法中断的那一行在其余行上显示错误位于该行之后。

用您的代码更改我的班级规则后,它会发出很多警告。我正在发送警告消息。

warning(200): ../org.xtext.example.myPoc/src-gen/org/xtext/example/mydsl/parser/antlr/internal/InternalMyPoc.g:416:2: Decision can match input such as "RULE_ID {RULE_ID..RULE_DATA_TYPE, RULE_DIRECTIVES_TYPE, 'Do'}" using multiple alternatives: 8, 9
As a result, alternative(s) 9 were disabled for that input
error(201): ../org.xtext.example.myPoc/src-gen/org/xtext/example/mydsl/parser/antlr/internal/InternalMyPoc.g:416:2: The following alternatives can never be matched: 9

warning(200): ../org.xtext.example.myPoc/src-gen/org/xtext/example/mydsl/parser/antlr/internal/InternalMyPoc.g:1166:1: Decision can match input such as "RULE_DIRECTIVES_TYPE '{' RULE_DIRECTIVES_TYPE '!,' RULE_STRING '}' RULE_DIRECTIVES_TYPE" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): ../org.xtext.example.myPoc/src-gen/org/xtext/example/mydsl/parser/antlr/internal/InternalMyPoc.g:1184:1: Decision can match input such as "'Do' RULE_ID '(' RULE_DATA_TYPE RULE_ID ',' RULE_DATA_TYPE RULE_ID ')' ';'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): ../org.xtext.example.myPoc/src-gen/org/xtext/example/mydsl/parser/antlr/internal/InternalMyPoc.g:1546:3: Decision can match input such as "RULE_DIRECTIVES_TYPE" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): ../org.xtext.example.myPoc.ui/src-gen/org/xtext/example/mydsl/ui/contentassist/antlr/internal/InternalMyPoc.g:568:1: Decision can match input such as "RULE_ID {RULE_ID, RULE_DATA_TYPE..RULE_DIRECTIVES_TYPE, '}', 'Do'..'('}" using multiple alternatives: 8, 9
As a result, alternative(s) 9 were disabled for that input
error(201): ../org.xtext.example.myPoc.ui/src-gen/org/xtext/example/mydsl/ui/contentassist/antlr/internal/InternalMyPoc.g:568:1: The following alternatives can never be matched: 9

warning(200): ../org.xtext.example.myPoc.ui/src-gen/org/xtext/example/mydsl/ui/contentassist/antlr/internal/InternalMyPoc.g:2810:42: Decision can match input such as "RULE_DIRECTIVES_TYPE '{' RULE_DIRECTIVES_TYPE '!,' RULE_STRING '}' RULE_DIRECTIVES_TYPE" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): ../org.xtext.example.myPoc.ui/src-gen/org/xtext/example/mydsl/ui/contentassist/antlr/internal/InternalMyPoc.g:2838:39: Decision can match input such as "'Do' RULE_ID '(' RULE_DATA_TYPE RULE_ID ',' RULE_DATA_TYPE RULE_ID ')' ';'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): ../org.xtext.example.myPoc.ui/src-gen/org/xtext/example/mydsl/ui/contentassist/antlr/internal/InternalMyPoc.g:3561:1: Decision can match input such as "RULE_DIRECTIVES_TYPE" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input

警告代码给出异常后

 GeneratorException: (Element: -UNKNOWN-; Reported by: Generator)
     org.eclipse.emf.common.util.WrappedException: java.io.FileNotFoundException: ..\org.xtext.example.myPoc\src-gen\org\xtext\example\mydsl\parser\antlr\internal\InternalMyPocLexer.java (The system cannot find the file specified)

我减少了语法并从代码中删除了所有错误和警告,但每一行中突出显示的错误仍然存​​在。现在我的语法是

grammar org.xtext.example.mydsl.MyPoc with org.eclipse.xtext.common.Terminals  hidden (GUESS_COMMENT,WS)

generate myPoc "http://www.xtext.org/example/mydsl/MyPoc"

Domainmodel:
    (elements+=Type)*;

Type:Class;

// This is the starting point of the grammar
 Class:
    (packageList+=packageList)*
    (packageList+=Import)*
    (directives+=Directives) 'class' name=ID ('extends' superType=[Class])? '{'
    (greetings+=Greeting 
    |features+=Feature 
    |setValues+=SetValues)*
    '}'; 


//Package list consist of all the packages we want to include in our class  
 packageList:
    ('package' name=ID) ('.'?) (classes+=[Class])?;

//Import list consist of all the packages we want to include in our class  
 Import:
    'import' importedNamespace=QualifiedNameWithWildcard;

QualifiedNameWithWildcard:
    'QualifiedName' '.*'?;

Greeting: 
(directives+=Directives)'Hello World'';'
(directives+=Directives)?'bye'?';'
(directives+=Directives)?';'
;

// Features defines the properties of that class 
// In our grammar by default we make the property private
// our grammar takes two types of DataType String or Integer 
// Name of the property given as per rule ID defined in Terminal file
Feature:
    (directives+=Directives)* type=DATA_TYPE name=ID ';';

    // In SetValues we initialize the properties by using SET directive
SetValues:
    (directives+=Directives) name=ID ('='?) (INT | STRING)';';

Directives:
    name=DirectiveType;

DirectiveType:
    {DirectiveType} value=DIRECTIVES_TYPE;

    // From here all are rules which shows that what we can use in our grammar 
terminal DATA_TYPE:
    ('Integer' | 'String');

// These are some directives used by us taken from there website
terminal DIRECTIVES_TYPE:
    ('SET' | 'WRITE' | 'READ' | 'QUIT' | 'If' | 'ElseIf' | 'Else' | 'EndIf' | 'Do' | 'while' | 'private' | 'public' | 'do');

terminal LOOP_CODITION_CHECK:
    ('=' | '>' | '<' | '!')+;

terminal GUESS_COMMENT : '//' !('\n'|'\r')* ('\r'? '\n');

在eclipse xtext编辑器中,当我将private写为privatee时,它在每一行都显示错误。我的代码是

public
class MyPoc {
    private
    String firstTitle ;
    **privatee**
    String title ;
    private
    String secondTitle ;
    private
    Integer firstAmount ;
    private
    Integer secondAmount ;
    SET
    firstTitle = "Ramesh";
    SET
    secondTitle = "Suresh";
    SET
    firstAmount = 100;
    SET
    secondAmount = 200; 

}

现在我在我的类属性中添加了更多语法。控制台没有显示警告,也没有异常。但是在 eclipse 编辑器中,当我们再次破坏语法语法时,它在每一行都显示错误。

Class:
    (packageList+=packageList)*
    (packageList+=Import)*
    (directives+=Directives) 'class' name=ID ('extends' superType=[Class])? LBRACKET
    (directives+=Directives* (features+=Feature | setValues+=SetValues | operations+=Operation | functionCall+=ArithmeticOperation | allMethod+=AllMethod
    | allMethodInClass+=AllMethodInClass | samples+=Samples) SEMICOLON)*
    RBRACKET;

如果我做错了什么,请检查一次并纠正我。

我发现问题不在类部分。问题出在我们在语法中定义规则的部分。就像ArithmeticOperation ArithmeticOperation 一样:

(指令+=指令)?名称=ID '='(指令+=指令)?('$')?types+=[操作] ('(' (args+=ID) ',' (args+=ID)* ')')?';';

实际上我必须在eclipse中编写这段代码。这里的Concat是操作名称意味着方法名称。

SET Result = WRITE $ Concat (firstTitle, secondTitle)

如果我做错了什么来实现我的目标代码,请纠正我。

问候

4

1 回答 1

2

Xtext 错误恢复系统与 Java Editor (JDT) 不同。它的行为取决于您如何编写规则。也许你的语法太模糊了,你应该添加一个行终止符号。

此外,您应该阅读这篇关于使用 Xtext 进行错误恢复的文章:Parser error recovery

编辑

目前,类规则与具有相同第一个标记的链接规则非常模棱两可。您应该做的第一件事是重写您的班级规则并删除所有歧义。(编译语法时的警告和错误)。请参见以下类规则示例:

 Class:
    (packageList+=packageList)*
    (packageList+=Import)*
    (directives+=Directives) 'class' name=ID ('extends' superType=[Class])? '{'
    (greetings+=Greeting | features+=Feature | setValues+=SetValues
        | operations+=Operation | functionCall+=ArithmeticOperation
        | directives+=Directive | allMethod+=AllMethod
        | allMethodInClass+=AllMethodInClass | samples+=Samples)*
    '}';

编辑 2 我对您的语法进行了一些重构。现在它具有您要求的行为。

    Domainmodel:
    (elements+=Type)*;

Type:Class;

// This is the starting point of the grammar
     Class:
        (packageList+=packageList)*
        (packageList+=Import)*
        (directives+=Directives) 'class' name=ID ('extends' superType=[Class])? LBRACKET
        (directives+=Directives* (features+=Feature | setValues+=SetValues) SEMICOLON)*
        RBRACKET; 


    terminal SEMICOLON:
        ';'
    ;

    terminal LBRACKET:
        '{'
    ;

    terminal RBRACKET:
        '}'
    ;

//Package list consist of all the packages we want to include in our class  
 packageList:
    ('package' name=ID) ('.'?) (classes+=[Class])?;

//Import list consist of all the packages we want to include in our class  
 Import:
    'import' importedNamespace=QualifiedNameWithWildcard;

QualifiedNameWithWildcard:
    'QualifiedName' '.*'?;


// Features defines the properties of that class 
// In our grammar by default we make the property private
// our grammar takes two types of DataType String or Integer 
// Name of the property given as per rule ID defined in Terminal file
Feature:
   type=DATA_TYPE name=ID ;

    // In SetValues we initialize the properties by using SET directive
SetValues:
    name=ID ('='?) (INT | STRING);

Directives:
    name=DirectiveType;

DirectiveType:
    {DirectiveType} value=DIRECTIVES_TYPE;

    // From here all are rules which shows that what we can use in our grammar 
terminal DATA_TYPE:
    ('Integer' | 'String');

// These are some directives used by us taken from there website
terminal DIRECTIVES_TYPE:
    ('SET' | 'WRITE' | 'READ' | 'QUIT' | 'If' | 'ElseIf' | 'Else' | 'EndIf' | 'Do' | 'while' | 'private' | 'public' | 'do');

terminal LOOP_CODITION_CHECK:
    ('=' | '>' | '<' | '!')+;

terminal GUESS_COMMENT : '//' !('\n'|'\r')* ('\r'? '\n');

我更改的详细信息: - 删除(没用?)问候规则。- 制作线/块分隔符终端。- 将指令移至父规则。

编辑器示例代码:

public
class MyPoc {
    private
    String firstTitle ;
    privatee
    String title ;
    private
    String secondTitle ;
    private
    Integer firstAmount ;
    private
    Integer secondAmount ;
    SET
    firstTitle = "Ramesh";
    SET
    secondTitle = "Suresh";
    SET
    firstAmount = 100;
    SET
    secondAmount = 200; 
}
于 2014-07-22T12:40:43.800 回答