Pascal subset lexical analyzer

Source: Internet
Author: User

Enter comments ^ o ^

Test with Pascal code snippets
Begin
Ab2a: = 9;
If x> = 0 then x: = x + 1;
While a = 0 do
B: = a * x/33455;
End
#

---------------------------------------------------------------------
Test Results
Syn | value
________ | ________
1 | begin
10 | ab2a
18 |: =
11 | 9
26 |;
2 | if
10 | x
24 |> =
11 | 0
3 | then
10 | x
18 |: =
10 | x
14 | +
11 | 1
26 |;
4 | while
10 |
25 | =
11 | 0
5 | do
10 | B
18 |: =
10 |
16 | *
10 | x
17 |/
11 | 33455
26 |;
6 | End
0 | #
Press any key to continue

The C-code parser of analyzer ------------------------------------------------------------------------------------------

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <ctype. h>
# Define max_wd_len 255.
# Define max_int 32767
# Define max_src_len 1000
# Define max_wd_cnt 100
# Define kwd_cnt 6
/*************************************** *********/

Union value_type {
Int D;
Char C;
Char s [MAX_WD_LEN];
};

Typedef struct {
Int syn;
Union value_type value;
} Word_type;

/*************************************** *********/
Char * keywords [20] = {"begin", "if", "then", "while", "do", "end "};
Char source [MAX_SRC_LEN];
Word_type word_stack [MAX_WD_CNT];
Int line = 1, wtop = 0, ip = 0;

/*************************************** *********/
Void p_word_stack (){
Int I; word_type w;
Printf ("syn/t | value/n ");
Printf ("________ | ________/n ");
For (I = 0; I <wtop; I ++ ){
W = word_stack [I];
If (w. syn> = 1 & w. syn <= 10) | w. syn = 18 | w. syn = 21 | w. syn = 22
| W. syn = 24)
Printf ("% d/t | % s/n", w. syn, w. value. s );
Else if (w. syn = 11)
Printf ("% d/t | % d/n", w. syn, w. value. d );
Else
Printf ("% d/t | % c/n", w. syn, w. value. c );
}
Return;
}
Void tell_err (){
Printf ("error in line % d/n", line );
Exit (1 );
Return;
}
Void scan (){
Word_type w;
Char c;
Int j = 0;
If (isdigit (c = source [ip]) {
W. syn = 11;/* dd **/
W. value. d = c-'0 ';
While (isdigit (c = source [++ ip])
W. value. d = w. value. d * 10 + c-'0 ';
If (! Isalpha (c ))
Word_stack [wtop ++] = w;
Else
Tell_err ();
Return;
}
If (isalpha (C = source [IP]) {
W. SYN = 10;/* (LL | D )*/
W. value. s [0] = C;
While (isalpha (C = source [++ IP]) | isdigit (c ))
W. value. s [++ J] = C;
W. value. s [J + 1] = '/0 ';
For (j = 0; j <kwd_cnt; j ++ ){
If (strcmp (keywords [j], w. value. s) = 0)
W. syn = j + 1;
}
Word_stack [wtop ++] = w;
Return;
}
Switch (c = source [ip]) {
Case '+ ':
W. syn = 14;/* '+ '*/
W. value. c = '+ ';
Word_stack [wtop ++] = w;
Ip ++;
Break;
Case '-':
W. syn = 15 ;/*'-'*/
W. value. c = '-';
Word_stack [wtop ++] = w;
Ip ++;
Break;
Case '*':
W. syn = 16 ;/*'*'*/
W. value. c = '*';
Word_stack [wtop ++] = w;
Ip ++;
Break;
Case '/':
W. syn = 17;
W. value. c = '/';
Word_stack [wtop ++] = w;
Ip ++;
Break;
Case ':':
W. SYN = 19;
W. value. c = ':';
If (C = source [++ IP])! = '){
Word_stack [wtop ++] = W;

}
Else if (C = '){
Strcpy (W. value. S, ": = ");
W. SYN = 18;
Word_stack [wtop ++] = W;
Ip ++;
}
Break;
Case '<':
W. syn = 20;
W. value. c = '<';
If (C = source [++ IP])! = '>' & C! = '){
Word_stack [wtop ++] = W;
}
Else if (C = '> '){
W. SYN = 21;
Strcpy (w. value. s, "<> ");
Word_stack [wtop ++] = w;
Ip ++;
}
Else if (c = '){
W. SYN = 22;
Strcpy (W. value. S, "<= ");
Word_stack [wtop ++] = W;
IP ++;
}
Break;
Case '> ':
W. syn = 23;
W. value. c = '> ';
If (c = source [++ ip])! = '){
Word_stack [wtop ++] = w;
}
Else if (C = '){
W. SYN = 24;
Strcpy (W. value. S, "> = ");
Word_stack [wtop ++] = W;
Ip ++;
}
Break;
Case '= ':
W. syn = 25;
W. value. c = ';
Word_stack [wtop ++] = w;
Ip ++;
Break;
Case ';':
W. syn = 26;
W. value. c = ';';
Word_stack [wtop ++] = W;
IP ++;
Break;
Case '(':
W. SYN = 27;
W. value. c = '(';
Word_stack [wtop ++] = W;
IP ++;
Break;
Case ')':
W. SYN = 28;
W. value. c = ')';
Word_stack [wtop ++] = w;
Ip ++;
Break;
Case '':
While (source [++ ip] = '');
Break;
Case '/N ':
Line ++;
While (source [++ ip] = '/N') line ++;
Break;

Case '/t ':
While (source [++ ip] = '/t ');
Break;
Case '/R ':
While (source [++ IP] = '/R ');
Break;
Default:
Tell_err ();
}
Return;
}
Int main (){
File * FP;
Int I = 0;
Word_type W;
Fp = fopen ("input.txt", "R ");
While (! Feof (FP ))
Source [I ++] = GETC (FP );
Fclose (FP );
While (source [IP]! = '#')
Scan (IP );
W. SYN = 0;
W. value. c = '#';
Word_stack [wtop ++] = W;
P_word_stack ();
}

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.