⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bison.texinfo

📁 bison源代码.bison 是替代yacc的语法分析程序生成器. yacc是 Yet Another Compiler Compiler的缩写. bison又是什么呐 是一个生成可以分析文本文件结构
💻 TEXINFO
📖 第 1 页 / 共 5 页
字号:
        | input line;@end exampleThis definition reads as follows: ``A complete input is either an emptystring, or a complete input followed by an input line''.  Notice that``complete input'' is defined in terms of itself.  This definition is saidto be @dfn{left recursive} since @code{input} appears always as theleftmost symbol in the sequence.  @xref{Recursion, ,Recursive Rules}.The first alternative is empty because there are no symbols between thecolon and the first @samp{|}; this means that @code{input} can match anempty string of input (no tokens).  We write the rules this way because itis legitimate to type @kbd{Ctrl-d} right after you start the calculator.It's conventional to put an empty alternative first and write the comment@samp{/* empty */} in it.The second alternate rule (@code{input line}) handles all nontrivial input.It means, ``After reading any number of lines, read one more line ifpossible.''  The left recursion makes this rule into a loop.  Since thefirst alternative matches empty input, the loop can be executed zero ormore times.The parser function @code{yyparse} continues to process input until agrammatical error is seen or the lexical analyzer says there are no moreinput tokens; we will arrange for the latter to happen at end of file.@node Rpcalc Line, Rpcalc Expr, Rpcalc Input, Rpcalc Rules@subsubsection Explanation of @code{line}Now consider the definition of @code{line}:@exampleline:     '\n'        | exp '\n'  @{ printf ("\t%.10g\n", $1); @};@end exampleThe first alternative is a token which is a newline character; this meansthat rpcalc accepts a blank line (and ignores it, since there is noaction).  The second alternative is an expression followed by a newline.This is the alternative that makes rpcalc useful.  The semantic value ofthe @code{exp} grouping is the value of @code{$1} because the @code{exp} inquestion is the first symbol in the alternative.  The action prints thisvalue, which is the result of the computation the user asked for.This action is unusual because it does not assign a value to @code{$$}.  Asa consequence, the semantic value associated with the @code{line} isuninitialized (its value will be unpredictable).  This would be a bug ifthat value were ever used, but we don't use it: once rpcalc has printed thevalue of the user's input line, that value is no longer needed.@node Rpcalc Expr,  , Rpcalc Line, Rpcalc Rules@subsubsection Explanation of @code{expr}The @code{exp} grouping has several rules, one for each kind of expression.The first rule handles the simplest expressions: those that are just numbers.The second handles an addition-expression, which looks like two expressionsfollowed by a plus-sign.  The third handles subtraction, and so on.@exampleexp:      NUM        | exp exp '+'     @{ $$ = $1 + $2;    @}        | exp exp '-'     @{ $$ = $1 - $2;    @}        @dots{}        ;@end exampleWe have used @samp{|} to join all the rules for @code{exp}, but we couldequally well have written them separately:@exampleexp:      NUM ;exp:      exp exp '+'     @{ $$ = $1 + $2;    @} ;exp:      exp exp '-'     @{ $$ = $1 - $2;    @} ;        @dots{}@end exampleMost of the rules have actions that compute the value of the expression interms of the value of its parts.  For example, in the rule for addition,@code{$1} refers to the first component @code{exp} and @code{$2} refers tothe second one.  The third component, @code{'+'}, has no meaningfulassociated semantic value, but if it had one you could refer to it as@code{$3}.  When @code{yyparse} recognizes a sum expression using thisrule, the sum of the two subexpressions' values is produced as the value ofthe entire expression.  @xref{Actions}.You don't have to give an action for every rule.  When a rule has noaction, Bison by default copies the value of @code{$1} into @code{$$}.This is what happens in the first rule (the one that uses @code{NUM}).The formatting shown here is the recommended convention, but Bison doesnot require it.  You can add or change whitespace as much as you wish.For example, this:@exampleexp   : NUM | exp exp '+' @{$$ = $1 + $2; @} | @dots{}@end example@noindentmeans the same thing as this:@exampleexp:      NUM        | exp exp '+'    @{ $$ = $1 + $2; @}        | @dots{}@end example@noindentThe latter, however, is much more readable.@node Rpcalc Lexer, Rpcalc Main, Rpcalc Rules, RPN Calc@subsection The @code{rpcalc} Lexical Analyzer@cindex writing a lexical analyzer@cindex lexical analyzer, writingThe lexical analyzer's job is low-level parsing: converting characters orsequences of characters into tokens.  The Bison parser gets its tokens bycalling the lexical analyzer.  @xref{Lexical, ,The Lexical Analyzer Function @code{yylex}}.Only a simple lexical analyzer is needed for the RPN calculator.  Thislexical analyzer skips blanks and tabs, then reads in numbers as@code{double} and returns them as @code{NUM} tokens.  Any other characterthat isn't part of a number is a separate token.  Note that the token-codefor such a single-character token is the character itself.The return value of the lexical analyzer function is a numeric code whichrepresents a token type.  The same text used in Bison rules to stand forthis token type is also a C expression for the numeric code for the type.This works in two ways.  If the token type is a character literal, then itsnumeric code is the ASCII code for that character; you can use the samecharacter literal in the lexical analyzer to express the number.  If thetoken type is an identifier, that identifier is defined by Bison as a Cmacro whose definition is the appropriate number.  In this example,therefore, @code{NUM} becomes a macro for @code{yylex} to use.The semantic value of the token (if it has one) is stored into the globalvariable @code{yylval}, which is where the Bison parser will look for it.(The C data type of @code{yylval} is @code{YYSTYPE}, which was definedat the beginning of the grammar; @pxref{Rpcalc Decls, ,Declarations for @code{rpcalc}}.)A token type code of zero is returned if the end-of-file is encountered.(Bison recognizes any nonpositive value as indicating the end of theinput.)Here is the code for the lexical analyzer:@example@group/* Lexical analyzer returns a double floating point    number on the stack and the token NUM, or the ASCII   character read if not a number.  Skips all blanks   and tabs, returns 0 for EOF. */#include <ctype.h>@end group@groupyylex ()@{  int c;  /* skip white space  */  while ((c = getchar ()) == ' ' || c == '\t')      ;@end group@group  /* process numbers   */  if (c == '.' || isdigit (c))                    @{      ungetc (c, stdin);      scanf ("%lf", &yylval);      return NUM;    @}@end group@group  /* return end-of-file  */  if (c == EOF)                                return 0;  /* return single chars */  return c;                                @}@end group@end example@node Rpcalc Main, Rpcalc Error, Rpcalc Lexer, RPN Calc@subsection The Controlling Function@cindex controlling function@cindex main function in simple exampleIn keeping with the spirit of this example, the controlling function iskept to the bare minimum.  The only requirement is that it call@code{yyparse} to start the process of parsing.@example@groupmain ()@{  yyparse ();@}@end group@end example@node Rpcalc Error, Rpcalc Gen, Rpcalc Main, RPN Calc@subsection The Error Reporting Routine@cindex error reporting routineWhen @code{yyparse} detects a syntax error, it calls the error reportingfunction @code{yyerror} to print an error message (usually but not always@code{"parse error"}).  It is up to the programmer to supply @code{yyerror}(@pxref{Interface, ,Parser C-Language Interface}), so here is the definition we will use:@example@group#include <stdio.h>yyerror (s)  /* Called by yyparse on error */     char *s;@{  printf ("%s\n", s);@}@end group@end exampleAfter @code{yyerror} returns, the Bison parser may recover from the errorand continue parsing if the grammar contains a suitable error rule(@pxref{Error Recovery}).  Otherwise, @code{yyparse} returns nonzero.  Wehave not written any error rules in this example, so any invalid input willcause the calculator program to exit.  This is not clean behavior for areal calculator, but it is adequate in the first example.@node Rpcalc Gen, Rpcalc Compile, Rpcalc Error, RPN Calc@subsection Running Bison to Make the Parser@cindex running Bison (introduction)Before running Bison to produce a parser, we need to decide how to arrangeall the source code in one or more source files.  For such a simple example,the easiest thing is to put everything in one file.  The definitions of@code{yylex}, @code{yyerror} and @code{main} go at the end, in the``additional C code'' section of the file (@pxref{Grammar Layout, ,The Overall Layout of a Bison Grammar}).For a large project, you would probably have several source files, and use@code{make} to arrange to recompile them.With all the source in a single file, you use the following command toconvert it into a parser file:@examplebison @var{file_name}.y@end example@noindentIn this example the file was called @file{rpcalc.y} (for ``Reverse PolishCALCulator'').  Bison produces a file named @file{@var{file_name}.tab.c},removing the @samp{.y} from the original file name. The file output byBison contains the source code for @code{yyparse}.  The additionalfunctions in the input file (@code{yylex}, @code{yyerror} and @code{main})are copied verbatim to the output.@node Rpcalc Compile,  , Rpcalc Gen, RPN Calc@subsection Compiling the Parser File@cindex compiling the parserHere is how to compile and run the parser file:@example@group# @r{List files in current directory.}% lsrpcalc.tab.c  rpcalc.y@end group@group# @r{Compile the Bison parser.}# @r{@samp{-lm} tells compiler to search math library for @code{pow}.}% cc rpcalc.tab.c -lm -o rpcalc@end group@group# @r{List files again.}% lsrpcalc  rpcalc.tab.c  rpcalc.y@end group@end exampleThe file @file{rpcalc} now contains the executable code.  Here is anexample session using @code{rpcalc}.@example% rpcalc4 9 +133 7 + 3 4 5 *+--133 7 + 3 4 5 * + - n              @r{Note the unary minus, @samp{n}}135 6 / 4 n +-3.1666666673 4 ^                            @r{Exponentiation}81^D                               @r{End-of-file indicator}%@end example@node Infix Calc, Simple Error Recovery, RPN Calc, Examples@section Infix Notation Calculator: @code{calc}@cindex infix notation calculator@cindex @code{calc}@cindex calculator, infix notationWe now modify rpcalc to handle infix operators instead of postfix.  Infixnotation involves the concept 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -