Homemade programming language Note: Using YACC with Lex to make a simple calculator

Source: Internet
Author: User
Tags mul

1. Code

1.1) TEST.L

1.2) Test.y

1.3) Makefile (because it is in a Linux environment, so Makefile is used)

2. Compiling and Running

2.1) Compiling

2.2) Run

1, the code (also can be in my Baidu network disk download:http://pan.baidu.com/s/1o65k7v8)

1.1) Lex File test.l

1%{2#include <stdio.h>3#include"Y.tab.h"4 5 int 6Yywrap (void)7 {8     return 1;9 }Ten%} One%% A "+"    returnADD; - "-"    returnSUB; - "*"    returnMUL; the "/"    returnDIV; - "\ n"   returnCR; -([1-9][0-9]*)|0| ([0-9]+\. [0-9]*) { -     Doubletemp; +SSCANF (Yytext,"%LF", &temp); -Yylval.double_value =temp; +     returndouble_literal; A } at [\ t]; - . { -fprintf (stderr,"lexical error.\n"); -Exit1); - } -%%

1.2) YACC file Text.y

1%{2#include <stdio.h>3#include <stdlib.h>4 #defineYydebug 15%}6%Union {7     intInt_value;8     DoubleDouble_value;9 }Ten%token <double_value>double_literal One%token ADD SUB MUL DIV CR A%type <double_value>Expression Term primary_expression -%% - line_list the : Line -|line_list Line -     ; -  Line + : Expression CR -     {    +printf">>%lf\n>", $1); A fflush (stdout); at     }    - expression - : term -|expression ADD term -     { -$$ = $1+ $3; in     } -|expression SUB term to     { +$$ = $1- $3; -     } the     ; *  Term $ : Primary_expressionPanax Notoginseng|Term MUL primary_expression -     { the$$ = $1* $3; +     } A|Term DIV primary_expression the     { +$$ = $1/ $3; -     } $     ; $ primary_expression - : Double_literal -     ; the%% - intWuyiYyerror (Char*str) the { -     extern Char*Yytext; Wufprintf (stderr,"parser Error near%s\n", yytext); -     return 0; About } $  - intMainvoid) - { -     extern intYyparse (void); A     externFILE *Yyin; +  theprintf">"); - fflush (stdout); $Yyin =stdin; the     if(Yyparse ()) { thefprintf (stderr,"Error! Error! Error!\n"); theExit1); the     } -}

1.3) Makefile file (in fact, do not makefile, I am here in order to compile and purge each time when used, the following commands can be executed in three steps manually, the original is manually executed)

 1  . Phony:dummy  2  3  all:dummy  4  yacc-DV test.y   5   Lex Test.l  6   GCC -o test lex.yy.c y.tab.c   7  8  clean:dummy  9  rm -rf lex.yy.c test y.output y.tab.c y.tab.h  

2. Compiling and Running

2.1) Compiling

1  Make 2 YACC-dv test.y3Lex test.l4gcc -o test lex.yy.c Y.TAB.C

2.2) Run

1 ls 2 lex.yy.c  Makefile  readme.txt  test  test.l  test.y  y.output  y.tab.c  y.tab.h3 $./4123-4+2*3/ 7 5 >>3.8571436 > q7 lexical error.

Source: "Self-made programming language" chapter II

Baidu Encyclopedia:http://baike.baidu.com/link?url= Wc3bgkho7gmeupbxrx8wsa6-kd69hlbrjd6tnmgpzncq9j7xtgzxxh7rpufn2ishiww6a8zri6qt_5xvif9vi_

Watercress:http://book.douban.com/subject/25735333/

Homemade programming language Note: Using YACC with Lex to make a simple calculator

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.