Compiling principle Experiment 1 pl/0 language lexical analysis

Source: Internet
Author: User
Tags strcmp

lexical analysis of pl/0 language

First, the purpose of the experiment

Through the completion of lexical analysis procedures, understand the process of lexical analysis. Compile a read Word program, the lexical analysis of the pl/0 language, the input string form of the source program into a word symbol, that is, the basic reserved words, identifiers, constants, operators, the five major categories.

Second, the experimental environment

Operating system:Windows XP

Authoring Environment:Visual C + + ,c-free,Turbo C

Writing language:C language

Analytical language:pl/0

Iii. contents of the experiment

Lexical analysis of the pl/0 language, the input string form of the source program is divided into a word symbol, the lexical description is as follows:

(1) Keywords:begin,Pager,Const, Do,End,if,Odd,procedure,Read, Then,var, while,Write

(2) Identifier: Used to denote various names, must begin with a letter less than ten characters

(3) Number: A number smaller than the 0-9

(4)   operator: + - * Span style= "font-family: Arial" >, / := < <= > Span style= "Font-family:calibri" >>=

(5) The interface symbol:,,. , ; , #

Table 1 Table of the corresponding types of Word symbols

Word symbols

Type

+

Plus

-

Minus

*

Times

/

Slash

(

Lparen

)

Rparen

=

Eql

,

Comma

.

Perio

#

Neq

;

Semicolon

Begin

Beginsym

Pager

Callsym

Const

Constsym

Do

Dosym

End

Endsym

If

Ifsym

Odd

Oddsym

Procedure

Proceduresym

Read

Readsym

Then

Thensym

Var

Varsym

While

Whilesym

Write

Writesym

Getsym function function:

(1) Filter space space in the lexical analysis is an indispensable interface, and in the parsing is useless, so must filter

(2) Identify the reserved word The main program defines a one-dimensional array of characters as elements WORD, which is called the reserved word table. This table is checked for letters and numeric strings that begin with letters. If the check is recognized as a reserved word, the corresponding category is placed in the SYM . If The corresponding value of if is ifsym, then the corresponding value is thensym. If not, the user-defined identifier is considered

(3) identify reserved words to user-defined identifiers put IDENT in SYM , the value of the identifier itself is placed in the ID

(4) When scanning to a number string, the decimal number in the form of a string is converted to a binary number, and the number is placed in the SYM , and the value itself is placed in NUM in

(5) The combination of compound words to two characters of the operator, such as:>=,: =,<= , etc., after the recognition of the category sent SYM in

(6) Output source program for the side read into the character edge output (can be output in the file)

Four

Experimental results

The content to be analyzed is as follows:

Const A=36;var c,d;procedure P;beginvar g;g:=1234567890000000;write (g); End;beginread (c,d); if C<=d then C:=a;write ( C,D); call p;    End.

Pl0.h header File

#define NORW 13/* keyword number */#define The maximum number of digits of Nmax 14//number #define The most long symbol of the Al 10//symbol is +-god horse's # define Cxmax 200//Maximum number of virtual machine code enum Symbol {nul, ident, number, plus, minus, times, Slash, Oddsym, EQL, NEQ,LSS, Leq, GTR, Geq, Lparen, Rparen, comma, Semicol On, period, Becomes,beginsym, Endsym, Ifsym, Thensym, Whilesym, Writesym, Readsym, Dosym, Callsym, Constsym, Varsym, procs YM,}; file* fa1;//Output Analysis of the file and first address is the virtual machine pointer char ch;//getch read character enum symbol Sym;char id[al+1];//current Identint num; int CC, LL; Getch counter int cx; Virtual machine code pointer, value range 0-cxmax-1 char Line[81];char a[al+1];//read a symbol temporarily present here Char word[norw][al];//reserved word 13 is the begin end if what the enum SY Mbol WSYM[NORW]; The symbol of the reserved word begin corresponds to the symbol value of the Beginsym enum symbol ssym[256];//single character file* fin; file* fout;char fname[al];//input filename int err; #define GETCHDOIF ( -1==getch ()) return-1;void error (int n);

Main function

#include <stdio.h> #include <string.h> #include "pl0.h" void error (int n) {char space[81];memset (space, 32, 81 ); Space[cc-1] = 0;printf ("Error \ n"), fprintf (FA1, "error \ n"); err++;}            int Getch () {if (cc = = ll) {if (feof (Fin)) {printf ("finished reading");        return-1;        } ll = 0;        CC = 0;        printf ("%d", CX);        fprintf (FA1, "%d", CX);        ch = ";                while (ch! =) {if (FSCANF (Fin, "%c", &ch) = = EOF) {Line[ll] = 0;            Break            } printf ("%c", ch);            fprintf (FA1, "%c", ch);            LINE[LL] = ch;        ll++;        } printf ("\ n");    fprintf (FA1, "\ n");    } ch = line[cc];    cc++; return 0;}    int Getsym () {int I, j, K;    while (ch = = "| | ch = = | | ch = = 9) {getchdo;        } if (ch >= ' a ' && ch <= ' Z ') {k = 0;      do {if (K < AL)      {A[k] = ch;            k++;        } Getchdo;            }while (Ch >= ' a ' && ch <= ' z ' | | ch >= ' 0 ' && ch <= ' 9 ');        A[k] = 0;        strcpy (ID, a);        i = 0;        j = norw-1;            do {k = (i+j)/2;            if (strcmp (ID, word[k]) <= 0) j = k-1;        if (strcmp (ID, word[k]) >= 0) i = k+1;                }while (i <= J);        if (I-1 > J) {sym = wsym[k];        } else {sym = ident;            }} else {if (ch >= ' 0 ' && ch <= ' 9 ') {k = 0;            num = 0;            sym = number;                do {num = num * + CH-' 0 ';                k++;            Getchdo;            }while (Ch >= ' 0 ' && ch <= ' 9 ');            k--;            if (k > Nmax) {error (30);  }        }      else {if (ch = = ': ') {getchdo;                    if (ch = = ' = ') {sym = becomes;                Getchdo;                } else {sym = nul;                    }} else {if (ch = = ' < ') {Getchdo;                        if (ch = = ' = ') {sym = Leq;                    Getchdo;                    } else {sym = LSS;                        }} else {if (ch = = ' > ') {                        Getchdo;                            if (ch = = ' = ') {sym = Geq;                        Getchdo;                  } else      {sym = gtr;                    }} else {sym = Ssym[ch];                    if (sym! = period) {Getchdo;                } else return-1;} }}}} return 0;} void init () {int i;for (int i = 0; i < i++) Ssym[i] = nul;ssym[' + '] = plus;ssym['-'] = minus;ssym[' * '] = times;ssym[ '/'] = slash;ssym[' ('] = lparen;ssym[') ' = rparen;ssym[' = '] = eql;ssym[', '] = comma;ssym['. '] = period;ssym[' # '] = Neq;ss Ym['; '] = semicolon;strcpy (& (Word[0][0]), "Begin"), strcpy (& (Word[1][0]), "call"), strcpy (& (Word[2][0]), "Const"), strcpy (& (Word[3][0]), "do"), strcpy (& (Word[4][0]), "End"), strcpy (& (Word[5][0]), "if"), strcpy ( & (Word[6][0]), "odd"), strcpy (& (Word[7][0]), "procedure"), strcpy (& (Word[8][0]), "read"), strcpy (& ( Word[9][0]), "then"), strcpy (& (Word[10][0]), "var") STRCPY (& (Word[11][0]), "while"), strcpy (& (Word[12][0]), "write"), wsym[0] = beginsym;wsym[1] = callsym;wsym[2] = CONSTSYM;WSYM[3] = dosym;wsym[4] = endsym;wsym[5] = ifsym;wsym[6] = oddsym;wsym[7] = procsym;wsym[8] = readsym;wsym[9] = t HENSYM;WSYM[10] = varsym;wsym[11] = whilesym;wsym[12] = writesym;} int main () {printf ("Enter file name to parse \ n"), scanf ("%s", fname), fin = fopen (fname, "R"), if (Fin) {printf ("Enter file name to save \ n"); scanf (" %s ", fname); fa1 = fopen (fname," w "); init (); err = 0;CC = CX = LL = 0;ch ="; while (Getsym ()! =-1) {}printf ("parse complete"); }else{printf ("File not found \ n");} printf ("\ n"); return 0;}


Compiling principle Experiment 1 pl/0 language lexical analysis

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.