问题
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