Lex and Yacc and EBNF specification

五迷三道 提交于 2019-12-11 20:30:56

问题


if I just want to check whether the syntax of a language is correct or not, what is the easy way of writing a syntax analyzer using yacc.


回答1:


Note that the ISO standard for EBNF is ISO 14977:1996 and the 'EBNF' you've used in the question bears limited resemblance to the standard version. That leaves us having to interpret your grammar rule.

  • Non-terminals are written as single words in all lower-case.
  • Terminals are written as single words in all upper-case.
  • Colon is used to separate a non-terminal from its definition.
  • Dot (period) is used to mark the end of a rule.
  • Square brackets enclose optional (zero or once) material.

With those definitions in mind, you need:

  • A lexical analyzer that recognizes DECLARATION, OF, CONST, VAR, END as terminals (keywords).
  • A grammar that contains rules for declaration_unit, ident, const_declaration, var_declaration, procedure_interface, function_interface.

Given:

%token DECLARATION
%token OF
%token CONST
%token VAR
%token END

%%

declaration_unit
    :   DECLARATION OF ident opt_const_declaration opt_var_declaration
        opt_procedure_interface opt_function_interface DECLARATION END
    ;

opt_const_declaration
    :   /* Nothing */
    |   CONST const_declaration
    ;

opt_var_declaration
    :   /* Nothing */
    |   VAR var_declaration
    ;

opt_procedure_interface
    :   /* Nothing */
    |   procedure_interface
    ;

opt_function_interface
    :   /* Nothing */
    |   function_interface
    ;

You now just have to fill in the rules for ident, const_declaration, var_declaration, procedure_interface, function_interface.

For simple syntax checking, you could add placeholder tokens and rules for the parts of the grammar that you've not yet fully defined. For example, you might add:

%token IDENT
%token CONST_DECLARATION
%token VAR_DECLARATION
%token PROCEDURE_INTERFACE
%token FUNCTION_INTERFACE

and

ident
    :   IDENT
    ;

const_declaration
    :   CONST_DECLARATION
    ;

var_declaration
    :   VAR_DECLARATION
    ;

procedure_interface
    :   PROCEDURE_INTERFACE
    ;

function_interface
    :   FUNCTION_INTERFACE
    ;

Your lexical analyzer simply needs to be able to recognize those dummy tokens reliably until you provide the correct rules.



来源:https://stackoverflow.com/questions/26133237/lex-and-yacc-and-ebnf-specification

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!