Preliminary study of Lex and YACC

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.