Lexical analyzer for simple C

Source: Internet
Author: User
Tags define null

This source code can be run in Linux !!! Recognize Decimals

 

# Include <unistd. h>
# Include <stdlib. h>
# Include <stdio. h>
# Include <string. h>
/* # Define null 0 */

/* Custom variables */
# Define sy_if 0
# Define sy_then 1
# Define sy_else 2
# Define sy_while 3
# Define sy_begin 4
# Define sy_do 5
# Define sy_end 6
# Define a 7
# Define semicolon 8
# Define e 9
# Define jinghao 10
# Define s 11
# Define L 12

# Define tempsy 15
# Define EA 18/* E end */
# Define EO 19/* E or */

# Define plus 34
# Define times 36
# Define becomes 38
# Define op_and 39
# Define op_or 40
# Define op_not 41
# Define ROP 42

# Define lparent 48 // left parenthesis
# Define rparent 49 // parentheses
# Define ident 56 // variable
# Define intconst 57

Char CH = '/0';/* store read current character */
Int COUNT = 0; // Number of buffer results for lexical analysis
Int count1 = 0;
Static char Spelling [10] = {""};/* store recognized words */
Static char line [81] = {""};/* read a line of characters to the buffer */
Char * pline;/* character buffer pointer */

/* Struct Array {
Int sy11;
Int pos11;
} A [100] cunfangcifajieguo */

Static char ntab1 [100] [10];/* store the variable table */

Struct rwords {
Char SP [10];
Int Sy ;};/* store reserved words. SP indicates reserved words, and SY indicates reserved words */

Struct rwords reswords [10] = {"if", sy_if },
{"Do", sy_do },
{"Else", sy_else },
{"While", sy_while },
{"Then", sy_then },
{"Begin", sy_begin },
{"End", sy_end },
{"And", op_and },
{"Or", op_or },
{"Not", op_not }};

Struct AA {int sy1; // type
Int Pos;/* Internal code value */
} Buf [1000];/* buffer source code for lexical analysis results */

Int nlength = 0;/* record the length of the variable table */

Int lnum = 0;/* source program length */

Int TT1 = 0;/* start position of the variable table */
File * cfile;
File * mfile;

/* Read a row from the file to the buffer */
Void Readline ()
{
Char success;
Pline = line;
Cursor = getchar (cfile); // windows
// Outputs = fgetc (cfile); // Linux
While (Bytes! = '/N ')
{* Pline = Beijing;
Pline ++;
Cursor = getchar (cfile); // windows
// Outputs = fgetc (cfile); // Linux
}
* Pline = '/0 ';
Pline = line;
}/* Read a line in the buffer */

/* Read a character from the buffer zone */
Void readch ()
{If (CH = '/0 ')
{Readline ();
Lnum ++;
}
Ch = * pline;
Pline ++;
}/* Read a char from buffer */

/* Identify (identifier) whether the string has been included in the variable table: if it does not exist, return-1 and vice versa */
Int find (char spel [])
{
Int SS1 = 0;
Int II = 0;
While (SS1 = 0) & (ii <nlength ))
{If (! Strcmp (spel, ntab1 [II])
SS1 = 1;
II ++; // sequential execution
}
If (SS1 = 1) return II-1;
Else return-1;
}

// Identify the identifier
Void identifier ()
{Int IIi = 0, J, K;
Int Ss = 0;
K = 0;
Do // only lowercase letters and numbers are recognized
{Spelling [k] = CH;
K ++;
Readch ();
} While (CH> = 'A') & (CH <= 'Z') | (CH> = '0 ') & (CH <= '9 ')));
Pline --; // rollback
Spelling [k] = '/0'; // Since K in do while has added a 1, the end character'/0' is used here.
While (Ss = 0) & (III <10) // confirm whether it is in reserved words (Search)
{If (! Strcmp (spelling, reswords [III]. SP) // returns 0 if strcmp is equal to or less
Ss = 1; // If the reserved word can be found, the SS value is changed to 1 and the loop exists.
III ++;
}
/* Guanjianzipipei */
If (Ss = 1) // if it is a reserved word
{Buf [count]. sy1 = reswords [III-1]. Sy; // place reserved words in the analysis result Buffer
}
Else
{Buf [count]. sy1 = ident; // identifier type
J = find (spelling );
If (j =-1) // if it does not exist
{Buf [count]. Pos = TT1; // here, the variable position in the variable table is used to mark the internal code value.
Strcpy (ntab1 [TT1], spelling); // copy
TT1 ++; // position 1
Nlength ++; // The length of the variable table plus one
}
Else Buf [count]. Pos = J; // if such a variable already exists, the incode value is the position of the first variable
}
Count ++; // The number of lexical analysis buffer results plus one
For (k = 0; k <10; k ++) Spelling [k] = ''; // clear
}/* Shuzideshibie */
/*
Void number () // recognition constant
{Int ivalue = 0;
Int digit;
// Convert characters to numbers
Do
{Digit = CH-'0'; // the size of [0-9]
Ivalue = ivalue * 10 + digit; // Constant Value
Readch (); // read the next character
} While (CH> = '0') & (CH <= '9 '));
Buf [count]. sy1 = intconst; // constant type
Buf [count]. Pos = ivalue; // Constant Value
Count ++;
Pline --; // rollback
}
*/

Void number ()
{Float invalue = 0.0, Index = 0.0, digit = 0.0;
Int flag = 0;
Do
{
If (CH = '.') & (flag = 0 ))
{
Flag ++;
Readch ();
Continue;
}
If (CH> = '0') & (CH <= '9 '))
{
If (FLAG)
{
Index = Index * 0.1;
Digit = CH-'0 ';
Invalue = invalue + digit * index;
}
Else
{
Digit = CH-'0 ';
Invalue = invalue * 10 + digit;
}
}
Readch ();
} While (CH> = '0') & (CH <= '9') | (CH = '.'));
Buf [count]. sy1 = intconst;
Buf [count]. Pos = invalue;
Count ++;
Pline --;
}

// Scan the main function
Void scan ()
{
While (Ch! = '~ ')
{Switch (CH)
{Case '': break;
Case 'A ':
Case 'B ':
Case 'C ':
Case 'D ':
Case 'E ':
Case 'F ':
Case 'G ':
Case 'H ':
Case 'I ':
Case 'J ':
Case 'K ':
Case 'l ':
Case 'M ':
Case 'N ':
Case 'O ':
Case 'p ':
Case 'q ':
Case 'r ':
Case's ':
Case 'T ':
Case 'U ':
Case 'V ':
Case 'W ':
Case 'X ':
Case 'y ':
Case 'Z ':
Identifier ();
Break;
Case '0 ':
Case '1 ':
Case '2 ':
Case '3 ':
Case '4 ':
Case '5 ':
Case '6 ':
Case '7 ':
Case '8 ':
Case '9 ':
Number ();
Break;
Case '<':
Readch ();
If (CH = ')
Buf [count]. Pos = 0;
Else
{If (CH = '>') BUF [count]. Pos = 4;
Else
{Buf [count]. Pos = 1;
Pline --;
}
}
Buf [count]. sy1 = ROP;
Count ++;
Break;
Case '> ':
Readch ();
If (CH = ')
{Buf [count]. Pos = 2;
}
Else
{Buf [count]. Pos = 3;
Pline --;
}
Buf [count]. sy1 = ROP;
Count ++;
Break;
Case '(':
Buf [count]. sy1 = lparent;
Count ++;
Break;
Case ')':
Buf [count]. sy1 = rparent;
Count ++;
Break;
Case '#':
Buf [count]. sy1 = jinghao;
Count ++;
Break;
Case '+ ':
Buf [count]. sy1 = plus;
Count ++;
Break;
Case '*':
Buf [count]. sy1 = times;
Count ++;
Break;
Case ':':
Readch ();
If (CH = ')
Buf [count]. sy1 = becomes;
Count ++;
Break;
Case '= ':
Buf [count]. sy1 = ROP;
Buf [count]. Pos = 5;
Count ++;
Break;
Case ';':
Buf [count]. sy1 = semicolon;
Count ++;
Break;
}
Readch ();
}
Buf [count]. sy1 =-1;
Buf [count]. Pos =-1;
}

Void disp1 ()
{Int temp1 = 0;
Printf ("/n ********** cifafenxijieguo ***********/N ");
For (temp1 = 0; temp1 <= count; temp1 ++)
{
Printf ("% d/T % d/N", Buf [temp1]. sy1, Buf [temp1]. Pos );
If (temp1 = 20)
{Printf ("press any key to continue.../N ");
Getchar ();
}
}
Getchar ();
}

Void disp4 ()

{Int temp1 = 0;
Mfile = fopen ("cf.txt", "W ");
Printf ("/n ******* zidongshenchengwenjian ******/N ");
For (temp1 = 0; temp1 <count; temp1 ++)
Fprintf (mfile, "% d/T % d/N", Buf [temp1]. sy1, Buf [temp1]. Pos );
Fprintf (mfile ,"~ /N ");
}

Void disp3 ()
{
Int tttt;
Mfile = fopen ("bl.txt", "W ");
Printf ("/n % d, % d/N", lnum, count );
Getchar ();
Printf ("/n ********** bianliaangbiao ***********/N ");
For (tttt = 0; tttt <TT1; tttt ++)
{Printf ("% d/T % s/n", tttt, ntab1 [tttt]);
Fprintf (mfile, "% d/T % s/n", tttt, ntab1 [tttt]);
}
Fprintf (mfile ,"~ /N ");
Getchar ();
}

Int main ()
{
If (cfile = fopen ("2.txt"," R "))! = NULL)
Printf ("Open/N ");
Else
Printf ("close/N ");
Readch ();
Printf ("% C/N", CH );
Scan ();
Disp1 ();
Disp4 ();
Disp3 ();
Return 0;
}

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.