Yacc Lex

xiaoxiao2021-03-06  39

C language tools lex and yacc Description http://www.chinaunix.net Author: xzh2002 Posted: 2004-11-06 21:48:09

Lex Tool ------- Lex Tool is a lexical analysis program generator that generates a word identification program based on the requirements of the term rule specification, which identifies the various words in the input text. 1, the structure of the Lex program - Define the section - Rule section - User subroutine part of which the rule part is necessary, definition, and user subroutine parts are optional. (1) Define the part definition section starts from "% {" symbol, terminates at "%}" symbol, which can be a C statement including an incrude statement, a statement statement. % {include "stdio.h" #include "y.tab.h" extern int lineno;%} (2) Rules part of the rule section starts at "%%" symbol, terminates at "%%" symbol, It is a lexical rule. The lexical rules consist of two parts: mode and action. The mode portion can be composed of any regular expression, and the action portion is composed of a C language statement, and these statements are used to process the matching mode. It should be noted that LEX stores identified words in YYTEXT [] character data, so the content of the array represents the content identified. %% [/ t] {;} [0-9] * /. [0-9] * /. [0-9] {SSCANF (YYTEXT, "% 1F", & yylval.val); Return Number } / N {lineno ; return '/ n';}. {RETURN YYTEX [0];} %% (3) User subroutine part of the subscriber section can include subroutines written in C language, and these subroutines It can be used in the previous action so that the purpose of simplifying programming can be achieved. Here is a LEX program fragment with a subscriber.

"/ *" Skipcmnts ();. / * Rest of rules * / %% Skipcmnts () {for (;;) {while (input ()! = '*'); If (Input ()! = '/' UNPUT (YYTEXT [YYLEN-1]); Else Return;} 2, the method of using the Lex tool first writes a LEX program vi lex.l% {#include "stdio.h"%} %% [/ n]; 0-9] Printf ("Interger:% S / N", YYTEXT); [0-9] * /. [0-9] Printf ("Float:% S / N", YYText); [A- ZA-Z] [A-ZA-Z0-9] * Printf ("Word:% S / N", YYTEXT) ;. Printf ("Other Symbol:% C / N", YYTEXT [0]); %% then Use Lex to convert Lex.l into C language programs $ lex lex.l Use the C language program generated by the above command to use the C language program and use the C compiler to compile LEX.YY.C into an executable REGN $ CC - C lex.yy.c $ cc lex.yy.o -ll -o regn The following can be used to identify the word $ VI TESTFILE X = 355 y = 113 p = x / y # ./regn

The syntax rules we have to use are a grammar rules related to the four computments, available BNF paradigm Description List: EXPR / N LIST EXPR / N EXPR: Number Expr Expr Expr - Expr Expr * Expr EXPR / EXPR (EXPR) Its meaning is LIST It is an expression sequence, and each of them has a new line. The expression is a value, or two expressions connected by the operator, and the expression enclosed in parentheses. Below is a YACC program fragment on the above synchronization rules. $ vi hoc.y% {#define yystype double%}% token number% left ' ' - '% left' * '' / '%% list: | list' / n '| list expr' / n '{ Printf ("/ t%. 8g / n", $ 2);}; expr: Number {$$ = $ 1;} | expr ' ' expr {$$ = $ 1 $ 3;} | evr '-' expr {$ $ = $ 1 - $ 3;} | evr '*' expr {$$ = $ 1 * $ 3;} | evr '/' expr {$$ = $ 1 / $ 3;} | '(' expr ')' {$$ = $ 2 } %% The above YACC program is actually its definition part and the rule part. In the YACC declaration section,% Token Number indicates that Number is a word symbol, and% LEFT indicates the left conjunction of the operation symbol, and '*' and '/' and priority are priorities of ' ' and '-'. high. At the rule part of the YACC program, the alternate rules are separated by '|', the action in the rules is actually a C statement sequence, where $ n (ie $ 1, $ 2, etc.) is the first component used to reference the rules. And $$ represents the return value of the entire rule. The following YACC program fragment is Main master program #include #include char * progName; int lineeno = 1; main (argc, argv) int Argc; char * argv []; {progName = Argv [0]; YYPARSE ();} Main main program calls the YYPARSE subroutine to process the input, and YyParse is also entered through the YyleX subroutine and reports an error message through the YYERROR subroutine.

转载请注明原文地址:https://www.9cbs.com/read-78671.html

New Post(0)