Because it is a non-computer undergraduate, so did not learn the principle of compiling, come in want to fill up the lessons, so bought this "home-made programming language", which introduced the Lex and YACC tools, so loaded up to try the next.
The original tool to parse the string or is very convenient, before know the regular after, I think this thing is very good, now have Lex and YACC, the regular can do things magnified, can do more rich things.
For example, write a simple to add the numbers in the string, the other ignored programs (said simple refers to the function, in fact, the tuning is not simple, haha, especially the%type written in%token's clerical error, tangled for a long time)
Put the code below.
Test.l
%{#include <stdio.h> #include "y.tab.h" intyywrap (void) {return 1;} %}%% "\ n" return CR; [0-9]+ {int temp; SSCANF (Yytext, "%d", &temp); Yylval.int_value = temp; return A;} [\ t];. {//other Wordsyylval.int_value = 0; return ERR;} %%
Test.y
%{#include <stdio.h> #include <stdlib.h>%}%union {int int_value;} %token <int_value> a b%token cr err//!! this is %type!! %type <int_value> expression other%%line_list : line | line_list line;line : expression cr {printf ("%d\n", $1);} | cr {printf ("exit.\n"); exit (0);}; expression : a | other | expression a {$$ = $1 + $ 2;} | expression other {$$ = $1 + $2;};o Ther: err {printf (".");}; %%intyyerror (CHAR&NBSP;CONST&NBSP;*STR) { extern char *yytext; fprintf (stderr, "parser error near %s\n", yytext); return 0;} Int main (void) { extern int yyparse (void); Extern file *yyin; yyin = stdin; if ( Yyparse ()) { fprintf (stderr, "Error ! Error ! error !\n "); exit (1); }}
Then use Lex and YACC to generate an executable file
YACC-DV Test.ylex TEST.LCC *.c
Let's take a look at the effect.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/73/B1/wKiom1YD5hLjBaNCAABCgAqCkjc437.jpg "title=" 1111. JPG "alt=" wkiom1yd5hljbancaabcgaqckjc437.jpg "/>
Well, that's good!
This article is from the "Liu Sida blog" blog, make sure to keep this source http://liusida.blog.51cto.com/10741377/1697959
Preliminary study of Lex and YACC