Reading dragon Book compiling Principles of Grammar analysis (6) ...

Source: Internet
Author: User

These two days are not more, mainly immediately to test three (16, 18, 20), there is no time to learn the compiler, and so on this period of time should be able to have a lot of time to engage in this, after I am ready to write before the Lexical Analyzer generator transformation, get a simple version of the regular engine out to play, but these are something , it seems that there is not much time today, but also to write the work of this unit ... The code is relatively simple, not much to explain ...

1#include <ctype.h>2#include <stdio.h>3#include <stdlib.h>4 5 voidParse_f ();6 voidparse_t ();7 voidparse_e ();8 voidError (Char*want,Chargot);9 Ten inti; One Char*str =0; A  - voidError (Char*want,Chargot) - { thefprintf (stderr,"compling this expression:\n%s\n", str); -     intj =i; -      while(j--) -fprintf (stderr," "); +fprintf (stderr,"^\n"); -fprintf (stderr,"Syntax error at position:%d\n" +             "\texpecting:%s\n" A             "\tbut got:%c\n", at I, want, got); -Exit (0); -     return; - } -  - voidParse_f () in { -     Charc =Str[i]; to     if(IsDigit (c)) { +i++; -         return; the     } *     if(c=='('){ $i++;Panax Notoginseng parse_e (); -c =Str[i]; the         if(c==')'){ +i++; A             return; the         } +Error ("\ ') \ '", c); -         return; $     } $Error ("\ ' 0-9\ ' or \ ' (\ '", c); -     return; - } the  - Wuyi voidparse_t () the { - Parse_f (); Wu     Charc =Str[i]; -      while(c=='*'|| c = ='/'){ Abouti++; $ Parse_f (); -c =Str[i]; -     } -     return; A } +  the voidparse_e () - { $ parse_t (); the     Charc =Str[i]; the      while(c = ='+'|| c = ='-'){ thei++; the parse_t (); -c =Str[i]; in     } the     return; the } About  the voidParse (Char*e) the { thestr =e; +i =0; - parse_e (); the     if(str[i]==' /')Bayi         return; theError ("\ ' +\ ' or ' \\0\ '", Str[i]); the     return; - } - /////////////////////////////////////////////// the //Your Job: the //Add Some code into the function parse_e () and the //parse_t to parse "-" and "/" correctly. the //When you finish your task, NO error message - //should be generated. the //enjoy!:-P the intMain (intargcChar**argv) the {94     //there is the following rules on an expression: the     //1. Every expression is represented as a string; the     //2. Integers is non-negative; the     //3. Integers is between 0-9.98     Char*e; About  -E ="(2)";101 Parse (e);102 103E ="(3+4*5))";104 Parse (e); the 106E ="(8-2)";107 Parse (e);108 109E ="(8-2)/3"; the Parse (e);111  the     return 0;113}

Reading dragon Book compiling Principles of Grammar analysis (6) ...

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.