-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.txt
112 lines (108 loc) · 2.39 KB
/
input.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
%{
#include<iostream>
#include<string>
#include<stdio.h>
#include<fstream>
using namespace std;
%}
digit [0-9]
nature [1-9]
letter [a-zA-Z]
id {letter}[{letter}|{digit}]^
integer {digit}{digit}^
floatnum [{digit}|{digit}^].[{digit}|{digit}^]
port ${digit}^
%%
"auto" {return "AUTO";}
"break" {return "BREAK";}
"case" {return "CASE"; }
"char" {return "CHAR"; }
"const" {return "CONST";}
"continue" {return "CONTINUE";}
"default" {return "DEFAULT"; }
"do" {return "DO";}
"double" {return "DOUBLE"; }
"enum" {return "ENUM"; }
"else" {return "ELSE";}
"float" {return "FLOAT"; }
"for" {return "FOR";}
"if" {return "IF";}
"int" {return "INT";}
"long" {return "LONG";}
"return" {return "RETURN";}
"short" {return "SHORT";}
"sizeof" {return "SIZEOF";}
"struct" {return "STRUCT"; }
"switch" {return "SWITCH"; }
"typedef" {return "TYPEDEF";}
"union" {return "UNION"; }
"unsigned" {return "UNSIGNED"; }
"void" {return "VOID";}
"while" {return "WHILE";}
{id} {yylval=install_id();return "IDENTIFIER";}
{port} {yylval=convert_port();return "PORT";}
{floatnum} {yylval=convert_float();return "FLOATNUM";}
{integer} {yylval=convert_int();return "INTEGER";}
"+=" {return "ADD_ASSIGN";}
"-=" {return "SUB_ASSIGN";}
"*=" {return "MUL_ASSIGN"; }
"/=" {return "DIV_ASSIGN";}
"%=" {return "MOD_ASSIGN"; }
">>" {return "RIGHT_OP"; }
"<<" {return "LEFT_OP"; }
"++" {return "INC_OP"; }
"--" {return "DEC_OP";}
"->" {return "PTR_OP"; }
"&&" {return "ADD_OP";}
"<=" {return "LE_OP";}
">=" {return "GE_OP";}
"==" {return "EQ_OP";}
"!=" {return "NE_OP";}
"!" {return ("!");}
"+" {return ("+");}
"-" {return ("-");}
"/" {return ("/");}
"%" {return ("%");}
"<" {return ("<");}
">" {return (">");}
":" {return ("SEMICOLON");}
"?" {return ("?");}
";" {return (";");}
"," {return (",");}
"[" {return ("[");}
"]" {return ("]");}
"{" {return ("{");}
"}" {return ("}");}
"=" {return ("=");}
( {return ("(");}
) {return (")");}
%%
struct TOKEN{
string nameOrVal;
string type;
long address;
};
template <class Type>
Type stringToNum(const string& str)
{
istringstream iss(str);
Type num;
iss >> num;
return num;
}
int convert_port(){
return stringToNum<int>(str.substr(1,str.length()-1));
}
int convert_int(){
return stringToNum<int>(str);
}
float convert_float(){
return stringToNum<float>(str);
}
int install_id(){//装载IDENTIFER符号表
}
int main()
{
yylex();
return 0;
}