Lexical analysis program ( Lexical Analyzer ) requirements:
- stream from a source program composed of character scan left to right
- identify the lexical meaning of the word ( Lexemes )
- Return word record (word class, the word itself)
- filtered spaces
- skip comments
- lexical errors found
Program Structure:
Input: character stream (input what way, what data structure stored)
deal with:
- Traverse (What traversal)
- lexical rules
Output: word stream (what output form)
- tuple
Word class:
1. Identifier (10)
2. unsigned (11)
3. Leave the word (the word one yard)
4. Operator (word one yard)
5. delimiter (word one yard)
Word symbols |
Species do not code |
Word symbols |
Species do not code |
begin |
1 |
: |
17 |
if |
2 |
:= |
18 |
then |
3 |
< |
20 |
while |
4 |
<= |
21 |
do |
5 |
<> |
22 |
end |
6 |
> |
23 |
l(l|d)* |
10 |
>= |
24 |
dd* |
11 |
= |
25 |
+ |
13 |
; |
26 |
- |
14 |
( |
27 |
* |
15 |
) |
28 |
/ |
16 |
# |
0 |
Lexical analysis program Code:
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
//关键字
string key[6]={"main","int","if","else","while","do"};
//关键字的种别码
int keyNum[6]={1,2,3,4,5,6};
//运算符和界符
string symbol[17]={"<",">","!=",">=","<=","==",",",";","(",")","{","}","+","-","*","/","="};
//char symbol[12]={'<','>','!=','>=','<=','==',',',';','(',')','{','} '}; int isSymbol ( string s) {// Analyzing operators and delimitersint NUM;int length; // save the program number of charactersString words [1000];// convert the characters into wordsString Letter [1000];// store the extracted character file22, 23};int symbolNum [17] = {7,8,9,10,11,12,13,14,15,16,17,18,19,20
// operators and delimiters kind as code
I int;
for (I = 0; I <. 17; I ++) {
IF (S == Symbol [I])
return symbolNum [I];
}
return 0;
}
// determines whether the digital
bool isNum (string s) {
iF (S> = "0" && S <= ". 9")
return to true;
}
// determines whether a letter
BOOL isLetter (String S)
{
iF (S> = "A" && S <= "Z")
return to true;
}
// determines whether the keyword is not returned kind code
int iskeyword (String S) {
int I;
for (I = 0; I <. 6; I ++) {
iF (S == Key [I])
return KEYNUM [I];
}
return 0;
}
// return type single character
int typeword (String STR) {
IF (STR> = "a" && STR <= "Z") // letters
return. 1;
IF (STR> = "0" && STR <= ". 9") // Digital
return 2;
if(str==">"||str=="="||str=="<"||str=="!"||str==","||str==";"||str=="("||str==")"||str=="{"||str=="}"
||str=="+"||str=="-"||str=="*"||str=="/") //判断运算符和界符
return 3;
}
string identifier(string s,int n){
int j=n+1;
int flag=1;
while(flag){
if(isNum(letter[j]) || isLetter(letter[j])){
s=(s+letter[j]).c_str();
if(isKeyWord(s)){
j++;
num=j;
return s;
}
j++;
}
else{
flag=0;
}
}
num=j;
return s;
}
string symbolStr(string s,int n){
int j=n+1;
string str=letter[j];
if(str==">"||str=="="||str=="<"||str=="!") {
s=(s+letter[j]).c_str();
j++;
}
num=j;
return s;
}
string Number(string s,int n){
int j=n+1;
int flag=1;
while(flag){
if(isNum(letter[j])){
s=(s+letter[j]).c_str();
j++;
}
else{
flag=0;
}
}
num=j;
return s;
}
void print(string s,int n){
cout<<"("<<s<<","<<n<<")"<<endl;
}
void TakeWord(){ //取单词
int k;
for(num=0;num<length;){
string str1,str;
str=letter[num];
k=typeword(str);
switch(k){
case 1:
{
str1=identifier(str,num);
if(isKeyWord(str1))
print(str1,isKeyWord(str1));
else
print(str1,0);
break;
}
case 2:
{
str1=Number(str,num);
print(str1,24);
break;
}
case 3:
{
str1=symbolStr(str,num);
print(str1,isSymbol(str1));
break;
}
}
}
}
int main(){
char w;
int i,j;
freopen("input.txt","r",stdin);
freopen("result.txt","w",stdout); //从控制台输出,而不是文本输出
length=0;
while(cin>>w){
if(w!=' '){
letter[length]=w;
++ length;
} // remove the program from spaces
}
TakeWord ();
// for (J = 0; J <length; J ++) {
// << COUT Letter [J] << endl;
//}
fclose (stdin) ; // close the file
fclose (stdout); // close the file
return 0;
}
Screenshot results: