码迷,mamicode.com
首页 > 其他好文 > 详细

yyparse() and yylex()

时间:2018-05-22 22:12:51      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:conflict   htm   get   lock   describes   block   could   represent   item   

Yacc 与 Lex 快速入门

 

yyparse() returns a value of 0 if the input it parses is valid according to the given grammar rules.

It returns a 1 if the input is incorrect and error recovery is impossible.

 

yyparse() does not do its own lexical analysis. In other words, it does not pull the input apart into tokens ready for parsing.

Instead, it calls a routine called yylex() everytime it wants to obtain a token from the input.

 

yylex() returns a value indicating the type of token that has been obtained.

If the token has an actual value, this value (or some representation of the value, for example, a pointer to a string containing the value) is returned in an external variable named yylval.

It is up to the user to write a yylex() routine that breaks the input into tokens and returns the tokens one by one to yyparse().

See Function section for more information on the lexical analyzer.

 

How yacc works

The input to yacc describes the rules of a grammar. yacc uses these rules to produce the source code for a program that parses the grammar.

You can then compile this source code to obtain a program that reads input, parses it according to the grammar, and takes action based on the result.

The source code produced by yacc is written in the C programming language. It consists of a number of data tables that represent the grammar, plus a C function named yyparse().

By default, yacc symbol names used begin with yy. This is an historical convention, dating back to yacc‘s predecessor, UNIX yacc.

You can avoid conflicts with yacc names by avoiding symbols that start with yy.

If you want to use a different prefix, indicate this with a line of the form:
%prefix prefix
at the beginning of the yacc input. For example:
%prefix ww
asks for a prefix of ww instead of yy. Alternatively, you could specify -p ww on the lex command line.
The prefix chosen should be 1 or 2 characters long; longer prefixes lead to name conflicts on systems that truncate external names to 6 characters during the loading process.
In addition, at least 1 of the characters in the prefix should be a lowercase letter
(because yacc uses an all-uppercase version of the prefix for some special names, and this has to be different from the specified prefix).
Note
Different prefixes are useful when two yacc-produced parsers are to be merged into a single program.
For the sake of convenience, however, the yy convention is used throughout this manual.
 

yyparse() and yylex()

标签:conflict   htm   get   lock   describes   block   could   represent   item   

原文地址:https://www.cnblogs.com/cpsmile/p/9074009.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!