基本数据类型: int,double,point?, char, bool
int: +-*/^|~&double: +-*/point: *bool: ==, !=, &&, ||, !数组: [const]语句: 条件,选择,循环,强制转移条件: if/else选择: switch case :循环: for函数: 类型+标示符+(*)+{*}模板: Template<*>结构体: struct,构造函数,析构函数,::注释: //分隔符: ;, {}包含: #include<>//输入输出流: cin, cout宏:Definestaticmain()保留字:int, double, char, Point/->/%, const, bool , !=, <,>,=, ==, &&, ||, !, ^, &, +,-,*,/,,|,^,~,[], 转移控制:if/else, switch/case/:, for, break, continue模板:Template<T>自定义数据类型:struct, ::, 句子隔断符:;,{}包含:#include<>宏:Define代码:
lex.h:
#define INT 0
#define DOUBLE 1#define CHAR 2#define POINT 3#define ARROW 4#define ADDR 5#define CONST 6#define BOOL 7#define FUNCTION 8#define IF 9#define ELSE 10#define SWITCH 11#define CASE 12#define COLON 13#define FOR 14#define BREAK 15#define CONTINUE 16#define TEMPLATE 17#define STRUCT 18#define STATIC 19#define DOUBLECOLON 20#define INCLUDE 21#define DEFINE 22#define IDENTIFIER 23#define LDOUBLE 24#define LINTEGER 25#define BOOLAND 26#define BOOLOR 27#define BOOLNOT 28#define ASSIGN 29#define EQ 30#define NE 31#define LT 32#define LE 33#define GT 34#define GE 35#define LPAREN 36#define RPAREN 37#define LBRACE 38#define RBRACE 39#define DOT 40#define COMMA 41#define PLUS 42#define MINUS 43#define MUL 44#define DIV 45#define BITOR 46#define BITXOR 47#define BITNOT 48#define BITAND 49 #define SQUOTE 50#define DQUOTE 51#define SEMICOLON 52#define RETURN 53#define ANNOTATION 54lex.l:
%{
#include "lex.h"#include <stdlib.h>extern int yylval; %}%%int {printf("It is a keyword %s\n",yytext); return INT;} double {printf("It is a keyword %s\n",yytext); return DOUBLE;} char {printf("It is a keyword %s\n",yytext); return CHAR;} Point {printf("It is a keyword %s\n",yytext); return (POINT); }const {printf("It is a keyword %s\n",yytext); return CONST;}bool {printf("It is a keyword %s\n",yytext); return BOOL;}function {printf("It is a keyword %s\n",yytext); return FUNCTION;}if {printf("It is a keyword %s\n",yytext); return (IF);}else {printf("It is a keyword %s\n",yytext); return (ELSE);}switch {printf("It is a keyword %s\n",yytext); return (SWITCH);}case {printf("It is a keyword %s\n",yytext); return (CASE);}for {printf("It is a keyword %s\n",yytext); return (FOR);}break {printf("It is a keyword %s\n",yytext); return (BREAK);}continue {printf("It is a keyword %s\n",yytext); return (CONTINUE);}Template {printf("It is a keyword %s\n",yytext); return (TEMPLATE);}struct {printf("It is a keyword %s\n",yytext); return (STRUCT);}static {printf("It is a keyword %s\n",yytext); return (STATIC);}#include {printf("It is a keyword %s\n",yytext); return (INCLUDE);}#define {printf("It is a keyword %s\n",yytext); return (DEFINE);}return {printf("It is a keyword %s\n",yytext); return (RETURN);}[ \t\n] {}[a-zA-Z_][a-zA-Z0-9_]* {printf("It is a identifier %s\n",yytext); return IDENTIFIER;}[0-9]+\.[0-9]* {printf("It is a literal double %s\n",yytext); return LDOUBLE;}[0-9]+ {printf("It is a literl integer %s\n",yytext); return LINTEGER;}"&&" {printf("It is a logical AND %s\n",yytext); return (BOOLAND);}"||" {printf("It is a logical OR %s\n",yytext); return (BOOLOR);}"!" {printf("It is a logical NOT %s\n",yytext); return BOOLNOT;}"=" {printf("It is a assignment %s\n",yytext); return (ASSIGN);}"==" {printf("It is a relop equalence %s\n",yytext); return (EQ);}"!=" {printf("It is a relop not equal %s\n",yytext); return (NE);}"<" {printf("It is a relop less %s\n",yytext); return (LT);}"<=" {printf("It is a relop less equal %s\n",yytext); return (LE);}">" {printf("It is a greater %s\n",yytext); return (GT);}">=" {printf("It is a greater equal %s\n",yytext); return (GE);}"(" {printf("It is a left parenthese %s\n",yytext); return (LPAREN);}")" {printf("It is a right parenthese %s\n",yytext); return (RPAREN);}"{" {printf("It is a left brace %s\n",yytext); return (LBRACE);}"}" {printf("It is a right brace %s\n",yytext); return (RBRACE);}"." {printf("It is a dot %s\n",yytext); return (DOT);}"," {printf("It is a comma %s\n",yytext); return (COMMA);}"+" {printf("It is a plus sign %s\n",yytext); return (PLUS);}"-" {printf("It is a minus sign %s\n",yytext); return (MINUS);}"*" {printf("It is a multiple sign %s\n",yytext); return (MUL);}"/" {printf("It is a division sign %s\n",yytext); return (DIV);}"|" {printf("It is a bit or %s\n",yytext); return (BITOR);}"^" {printf("It is a bit xor %s\n",yytext); return (BITXOR);}"~" {printf("It is a bit not %s\n",yytext); return (BITNOT);}"&" {printf("It is a bit and %s\n",yytext); return (BITAND);} "::" {printf("It is a keyword %s\n",yytext); return (DOUBLECOLON);}"->" {printf("It is a arrow %s\n",yytext); return ARROW;}"//" {printf("It is a annotation %s\n",yytext); return ANNOTATION;}"%" {printf("It is a percentage %s\n",yytext); return ADDR;}"\"" {printf("It is a double quote %s\n",yytext); return SQUOTE;}"\'" {printf("It is a single quote %s\n",yytext); return DQUOTE;}":" {printf("It is a colon %s\n",yytext); return (COLON);}";" {printf("It is a semicolon %s\n",yytext); return (SEMICOLON);}. {printf("Unknown token!\n"); yyterminate(); }%%int yywrap(void){ return 0;}int main(int argc,char* argv[]){ argc--; argc++; while(1) yylex(); return 0;}