博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
词法分析
阅读量:5063 次
发布时间:2019-06-12

本文共 5341 字,大约阅读时间需要 17 分钟。

基本数据类型:    int,double,point?, char, bool

int:            +-*/^|~&
double:            +-*/
point:            *
bool:             ==, !=, &&, ||, !
数组:            [const]
语句:            条件,选择,循环,强制转移
条件:            if/else
选择:            switch case :
循环:            for
函数:            类型+标示符+(*)+{*}
模板:            Template<*>
结构体:        struct,构造函数,析构函数,::
注释:            //
分隔符:        ;, {}
包含:            #include<>
//输入输出流:    cin, cout
宏:Define
static
main()
保留字: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 54

lex.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;
}

转载于:https://www.cnblogs.com/heracles123/archive/2013/04/09/3010776.html

你可能感兴趣的文章
PHP的SQL注入技术实现以及预防措施
查看>>
软件目录结构规范
查看>>
mysqladmin
查看>>
解决 No Entity Framework provider found for the ADO.NET provider
查看>>
设置虚拟机虚拟机中fedora上网配置-bridge连接方式(图解)
查看>>
HEVC播放器出炉,迅雷看看支持H.265
查看>>
[置顶] Android仿人人客户端(v5.7.1)——人人授权访问界面
查看>>
Eclipse 调试的时候Tomcat报错启动不了
查看>>
ES6内置方法find 和 filter的区别在哪
查看>>
Android入门之文件系统操作(二)文件操作相关指令
查看>>
Android实现 ScrollView + ListView无滚动条滚动
查看>>
java学习笔记之String类
查看>>
UVA 11082 Matrix Decompressing 矩阵解压(最大流,经典)
查看>>
jdk从1.8降到jdk1.7失败
查看>>
硬件笔记之Thinkpad T470P更换2K屏幕
查看>>
【知识库】-数据库_MySQL 的七种 join
查看>>
iOS开发——缩放图片
查看>>
HTTP之URL的快捷方式
查看>>
满世界都是图论
查看>>
配置链路聚合中极小错误——失之毫厘谬以千里
查看>>