Here the lexical analysis module is nearing the end, the following analysis of the Word method is summarized:
Recursive descent is essentially a top-down analysis algorithm, suitable for manual coding, which is relatively more flexible, and can be more flexible for specific DSL processing optimization, but relatively slow. The auto generator is the opposite, followed by an instance of the LR (1) syntax generator-YACC.
First this is the structure of the source file required by YACC, by entering this file Yacc can generate the actual parser code for us ...
It can be seen from the figure that the file is divided into three parts, the following is the instance code:
1%{2#include <stdio.h>3#include <stdlib.h>4 intYylex ();5 voidYyerror (Char*err);6%}7 8%left'+'9 Ten%% One A Lines:line -|line lines; - theLine:exp'\ n'; - exp:n -| Exp'+'exp; - +N:'1' -|'2' +|'3' A|'4' at|'5' -|'6' -|'7' -|'8' -|'9' -|'0' ; in -%% to + intYylex () { - returnGetChar (); the } * $ voidYyerror (Char*err) {Panax Notoginsengprintf"%s\n", err); - } the + intMain (intargcChar*argv[]) { A yyparse (); the return 0; +}
Here is the output structure:
[Wind @gcc~] $./a.out2+3+43+4 2+
That's about it.
Reading dragon Book compiling Principles of Grammar Analysis (15) ...