Part of the code from MOOC
#include <ctype.h> #include <stdio.h> #include <stdlib.h>void parse_f (); void parse_t (); void Parse_e ( ); void error (char *want, Char got); int I;char *str = 0;void error (char *want, Char got) {fprintf (stderr, "compling thi S expression:\n%s\n ", str); int j = i; while (j--) fprintf (stderr, ""); fprintf (stderr, "^\n"); fprintf (stderr, "Syntax error at position:%d\n" "\texpecting:%s\n" "\tbut Got:%c\n", I , want, got); Exit (0); return;} void Parse_f () {char c = str[i]; if (IsDigit (c)) {i++; Return } if (c== ' (') {i++; Parse_e (); c = Str[i]; if (c== ') ') {i++; Return } error ("\ ') \ '", c); Return } error ("\ ' 0-9\ ' or \ ' (\ '", c); return;} void parse_t () {parse_f (); char C = str[i]; while (c== ' * ' | | c = = '/') {i++; Parse_f (); c = Str[i]; } return; void Parse_e () {parse_t (); char C = str[i]; while (c== ' + ' | | c = = '-') {i++; parse_t (); c = Str[i]; } return; void PArse (char *e) {str = e; i = 0; Parse_e (); if (str[i]== ') return; Error ("\ ' +\ ' or ' \\0\ '", Str[i]); return;} Your job://Add Some code into the function parse_e () and//parse_t to P Arse "-" and "/" correctly.//when you finish your tasks, NO error message//should be generated.//enjoy! :-P int Main (char argc, char **argv) {//There is the following rules on an expression://1. Every expression is represented as a string; 2. integers is non-negative; 3. Integers is between 0-9. Char *e; E = "(2)"; Parse (e); E = "(3+4*5))"; Parse (e); E = "(8-2) * *"; Parse (e); E = "(8-2)/3"; Parse (e); return 0;}
with June
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Compiler practice three syntax parser for arithmetic expressions