In-depth compilation principles-5-C language implementation of a simple syntax analyzer

Source: Internet
Author: User

Introduction

We have already introduced the implementation of the compiler preprocessing, lexical analysis, and lexical analyzer, as well as the task and process of syntax analysis.

The input of syntax analysis is a sequence of lexical units. Then, according to the syntax representation (expansion) of the language, the finite state machine theory is used to generate an abstract syntax tree and traverse the intermediate code, namely, three address codes. This section describes the internal implementation mechanism of the syntax analyzer in an experiment.

 

5.1 experiment description

Compile a recursive descent analysis program to check the syntax and structure of word sequences provided by the lexical analysis program.

C language is used to compile a recursive descent analysis program, and syntax analysis is performed on a simple language.

5.1.1 syntax of the simple language to be analyzed

The expanded BNF is shown as follows:

(1) <program >::= begin <statement string> end

(2) <statement string >::=< statement >{; <statement>}

(3) <statement >::= <value assignment statement>

(4) <value assignment statement >::= ID :=< expression>

Operator <expression >::= <item >{+ <item >|- <item>}

Condition <item >::= <factor >{* <factor >|/ <factor>

Latency <factor >:= ID | NUM | (<expression>)

. 2 lab requirements

Enter a word string and end with "#". If the syntax is correct, the success message is output and "success" is printed; otherwise, an error is output ".

For example:

Enter begin a: = 9; x: = 2*3; B: = a + x end #

Output success!

Input x: = a + B * c end #

Output error

 

5.2 C language code implementation

The core idea is to analyze the status step by step from the start state to the syntax expansion period until the analysis is complete. If the status does not match during this period, that is, the analysis is stopped due to a syntax error. Of course, there must be an error recovery mechanism in the actual syntax analyzer to find other syntax errors. That is, multiple syntax errors are reported at a time. It should be noted that in order to implement syntax analysis, we must first perform lexical analysis. Therefore, this Code contains the content in the previous section and the lexical analysis section.

[Html]
# Include "stdio. h"
# Include "string. h"
 
 
Char prog [100], token [8], ch;
Char * rwtab [6] = {"begin", "if", "then", "while", "do", "end "};
Int syn, p, m, n, sum;
Int kk;
 
Void factor (void );
Void expression (void );
Void yucu (void );
Void term (void );
Void statement (void );
Void lrparser (void );
Void scaner (void );
 
 
Int main (void)
{
P = kk = 0;
Printf ("\ nplease input a string (end with '#'): \ n ");
 
Do
{
Scanf ("% c", & ch );
Prog [p ++] = ch;
} While (ch! = '#');
 
P = 0;
Scaner ();
Lrparser ();
// Getch ();
}
 
Void lrparser (void)
{
If (syn = 1)
{
Scaner ();/* read the next word symbol */
Yucu ();/* call the yucu () function ;*/
 
If (syn = 6)
{
Scaner ();
If (syn = 0) & (kk = 0 ))
Printf ("success! \ N ");
}
Else
{
If (kk! = 1) printf ("the string haven't got a 'end '! \ N ");
Kk = 1;
}
}
Else
{
Printf ("haven't got a 'begin '! \ N ");
Kk = 1;
}

Return;
}
 
Void yucu (void)
{
Statement ();/* call the function statement ();*/
 
While (syn = 26)
{
Scaner ();/* read the next word symbol */
If (syn! = 6)
Statement ();/* call the function statement ();*/
}

Return;
}
 
Void statement (void)
{
If (syn = 10)
{
Scaner ();/* read the next word symbol */
If (syn = 18)
{
Scaner ();/* read the next word symbol */
Expression ();/* call the function statement ();*/
}
Else
{
Printf ("the sing ': =' is wrong! \ N ");
Kk = 1;
}
}
Else
{
Printf ("wrong sentence! \ N ");
Kk = 1;
}

Return;
}
 
Void expression (void)
{
Term ();
 
While (syn = 13) | (syn = 14 ))
{
Scaner ();/* read the next word symbol */
Term ();/* call the function term ();*/
}

Return;
}
 
Void term (void)
{
Factor ();
 
While (syn = 15) | (syn = 16 ))
{
Scaner ();/* read the next word symbol */
Factor ();/* call the function factor ();*/
}

Return;
}
 
Void factor (void)
{
If (syn = 10) | (syn = 11 ))
{
Scaner ();
}
Else if (syn = 27)
{
Scaner ();/* read the next word symbol */
Expression ();/* call the function statement ();*/
 
If (syn = 28)
{
Scaner ();/* read the next word symbol */
}
Else
{
Printf ("the error on '(' \ n ");
Kk = 1;
}
}
Else
{
Printf ("the expression error! \ N ");
Kk = 1;
}

Return;
}
Void scaner (void)
{
Sum = 0;
 
For (m = 0; m <8; m ++)
Token [m ++] = NULL;

M = 0;
Ch = prog [p ++];

While (ch = '')
Ch = prog [p ++];

If (ch <= 'Z') & (ch> = 'A') | (ch <= 'Z ') & (ch> = 'A ')))
{
While (ch <= 'Z') & (ch> = 'A') | (ch <= 'Z ') & (ch> = 'A') | (ch> = '0') & (ch <= '9 ')))
{
Token [m ++] = ch;
Ch = prog [p ++];
}
P --;
Syn = 10;
Token [m ++] = '\ 0 ';
For (n = 0; n <6; n ++)
If (strcmp (token, rwtab [n]) = 0)
{
Syn = n + 1;
Break;
}
}
Else if (ch> = '0') & (ch <= '9 '))
{
While (ch> = '0') & (ch <= '9 '))
{
Sum = sum * 10 + ch-'0 ';
Ch = prog [p ++];
}
P --;
Syn = 11;
}
Else
Switch (ch)
{
Case '<':
M = 0;
Ch = prog [p ++];
If (ch = '> ')
{
Syn = 21;
}
Else if (ch = ')
{
Syn = 22;
}
Else
{
Syn = 20;
P --;
}
Break;

Case '> ':
M = 0;
Ch = prog [p ++];
If (ch = ')
{
Syn = 24;
}
Else
{
Syn = 23;
P --;
}
Break;

Case ':':
M = 0;
Ch = prog [p ++];
If (ch = ')
{
Syn = 18;
}
Else
{
Syn = 17;
P --;
}
Break;

Case '+ ':
Syn = 13;
Break;

Case '-':
Syn = 14;
Break;

Case '*':
Syn = 15;
Break;

Case '/':
Syn = 16;
Break;

Case '(':
Syn = 27;
Break;

Case ')':
Syn = 28;
Break;

Case '= ':
Syn = 25;
Break;

Case ';':
Syn = 26;
Break;

Case '#':
Syn = 0;
Break;

Default:
Syn =-1;
Break;
}
}

Related Article

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.