0

我有以下解析代码,但我在非终端表达式和函数调用之间遇到了移位/减少冲突。这是 Visual Basic 编程语言的一小段解析代码。CFG会错吗?还是仅在这两个非终端符号中存在问题。

program ::= function_declarations:functionDeclarations
            | /* Vacio */
;


function_declarations ::= function_declaration:functionDeclaration function_declarations:functionDeclarations
;

function_declaration ::= structure_statement:structureStatement
             |sub_statement:subStatement
             |function_statement:functionStatement
;

structure_statement ::= TK_STRUCTURE TK_ID statements:stmnts TK_END TK_STRUCTURE
;

sub_statement ::= TK_SUB TK_ID TK_PARIN parameters:prmts TK_PAROUT statements:stmnts TK_END TK_SUB
;


function_statement ::=  TK_FUNCTION TK_ID TK_PARIN parameters:prmts TK_PAROUT statements:stmnts TK_END TK_FUNCTION
            |TK_FUNCTION TK_ID TK_PARIN parameters:prmts TK_PAROUT TK_AS data_type:type statements:stmnts TK_END TK_FUNCTION
;

parameters ::=  parameter:prmt TK_COMMA parameters:prmts
        |parameter:prmt
        | /* Vacio */
;

parameter ::=   parameter_type:prmt_type TK_ID TK_AS data_type:type
;

parameter_type ::=  TK_BYVAL
                    | TK_BYREF
;

data_type ::=   TK_INTEGER
                |TK_BOOLEAN
                |TK_STRING
;

statements ::=  statement:stmnt statements:stmnts
                | /* Vacio */
;

statement ::=   if_statement:ifStatement
                |while_statement:whileStatement
                |for_statement:forStatement
                |do_statement:doStatement
                |variable_declaration:var_declare
                |expression:expr
                |TK_RETURN expression
                |TK_EXIT
;

if_statement ::=    TK_IF expression:expr TK_THEN statements:stmnts TK_END TK_IF
                    |TK_IF expression:expr TK_THEN statements:stmnts TK_ELSE statements:stmnts TK_END TK_IF
                    |TK_IF expression:expr TK_THEN statements:stmnts elseif_statements:elseifStmnts TK_END TK_IF
                    |TK_IF expression:expr TK_THEN statements:stmnts elseif_statements:elseifStmnts TK_ELSE statements:stmnts TK_END TK_IF
; 

elseif_statements ::=   elseif_statement:elseifStmnt elseif_statements:elseifStmnts
                        |elseif_statement:elseifStmnt
;

elseif_statement ::=    TK_ELSEIF expression:expr TK_THEN
;


for_statement ::=   TK_FOR variable_declaration:var_declare TK_TO expression:expr statements:stmnts TK_NEXT
                    |TK_FOR variable_declaration:var_declare TK_TO expression:expr TK_STEP TK_NUMBER statements:stmnts TK_NEXT
;

while_statement ::= TK_WHILE expression:expr statements:stmnts TK_END TK_WHILE
;

do_statement ::=    TK_DO TK_WHILE expression:expr statements:stmnts TK_LOOP
                    |TK_DO statements:stmnts TK_LOOP TK_UNTIL expression:expr
;

variable_declaration ::=    variable_declarator:var TK_COMMA variable_declaration:var_declare 
                            |variable_declarator:var
;


variable_declarator  ::=    TK_DIM TK_ID TK_AS data_type:type TK_EQUALS expression:expr
                |TK_ID TK_AS data_type:type TK_EQUALS expression:expr
                |TK_DIM TK_ID TK_EQUALS expression:expr
                |TK_ID TK_EQUALS expression:expr
                |TK_DIM TK_ID TK_AS TK_NEW data_type:type TK_EQUALS expression:expr TK_PARIN arguments:args TK_PAROUT
                |TK_ID TK_EQUALS TK_NEW data_type:type TK_PARIN arguments:args TK_PAROUT
;



expression ::=  numeric_expression:num_exp
                |boolean_expression:bool_exp
                |logical_expression:logic_exp
                |literal_expression:lit_exp
                |TK_ID:id
                |function_call:call
;

boolean_expression ::= expression:e1 TK_GREATERTHAN expression:e2
                       |expression:e1 TK_LESSTHAN expression:e2
                       |expression:e1 TK_GREATEREQUAL expression:e2
                       |expression:e1 TK_LESSEQUAL expression:e2
                       |expression:e1 TK_EQUALS expression:e2
;

logical_expression ::= TK_NOT expression:e1
                       |expression:e1 TK_OR expression:e2
                       |expression:e1 TK_AND expression:e2
                       |expression:e1 TK_XOR expression:e2
                       |TK_TRUE
                       |TK_FALSE
;
numeric_expression ::= expression:e1 TK_SUM expression:e2
                       |expression:e1 TK_MINUS expression:e2
                       |expression:e1 TK_PRODUCT expression:e2
                       |expression:e1 TK_DIVISION expression:e2
;

literal_expression ::=  TK_NUMBER
                        |TK_STRINGVAL
;

function_call ::=   TK_ID TK_PARIN arguments:args TK_PAROUT
                    |TK_ID TK_PARIN TK_PAROUT
;

arguments ::= argument TK_COMMA expression:expr
;

argument ::= expression:expr
;

什么可能给我带来麻烦?

4

1 回答 1

0

问题是没有什么能将一个陈述与下一个陈述分开。Anexpression是一个有效的语句,所以两个连续的表达式是一个有效的statements。但是,那么,一个单一的陈述也是如此。

现在 anID是 anexpression并且带括号的表达式是 aen 表达式。first. (second)两个 s 也是如此statement,¿ 不是吗?或者它是一个函数调用?在第一种情况下,您需要减少IDexpression(然后statement)。在第二种情况下,您只需将(.

因此,一个班次减少冲突。

于 2016-09-16T07:14:10.990 回答