zoukankan      html  css  js  c++  java
  • 词法分析

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    typedef struct{
    int num;
    char *word;
    }WORD; //存储种别码,单词
    char *rword[]={"begin","if","then","while","do","end","he"};
    char ci;
    int ainput,toke; char input[300];char token[300]=" ";

    char mgetch(){ //读取一个字符
    ci=input[ainput];
    ainput=ainput+1;
    return (ci);
    }

    void getbc(){ //去空格
    while(ci==' '||ci==10)
    { ci=input[ainput];
    ainput+=1;

    }
    }
    int letter() //判断是否为字母
    { if(ci>='a'&&ci<='z'||ci>='A'&&ci<='Z')
    return 1;
    else
    return 0;
    }
    int number() //判断是否为数字
    {
    if(ci>='0'&&ci<='9')
    return 1;
    else
    return 0;
    }
    int reserve() // 判断是否为关键字
    {
    int i=0;
    while(strcmp(rword[i],"he"))
    {
    if(!strcmp(rword[i],token))
    {
    return i+1;
    }
    i++;
    }

    return 10;

    }

    void link() //链接单词
    {
    token[toke]=ci;
    toke+=1;
    token[toke]='';

    }
    void back(){

    ainput-=1;
    }


    WORD * scaner(){
    WORD *myword;
    myword=(WORD*)malloc(sizeof(WORD));
    myword->num=10;
    myword->word="";
    toke=0;
    mgetch();
    getbc();
    if(letter()){
    while(letter()||number())
    {
    link();
    mgetch();
    }
    back();
    myword->num =reserve();
    myword->word=token;
    return(myword);
    }
    else if(number()) {
    while(number())
    {
    link();
    mgetch();

    }
    back();
    myword->num=11;
    myword->word=token;
    return(myword);
    }
    else switch(ci){
    case '=': mgetch();
    if(ci=='='){
    myword->num=39;
    myword->word="==";
    return(myword);
    }
    back();
    myword->num=25;
    myword->word="=";
    return(myword);
    break;


    case'+': myword->num=13;
    myword->word="+";
    return(myword);
    break;
    case'-': myword->num=14;
    myword->word="-";
    return(myword);
    break;
    case'*': myword->num=15;
    myword->word="*";
    return(myword);
    break;
    case'/': myword->num=16;
    myword->word="/";
    return(myword);
    break;
    case'(': myword->num=27;
    myword->word="(";
    return(myword);
    break;
    case')': myword->num=28;
    myword->word=")";
    return(myword);
    break;
    case'[': myword->num=29;
    myword->word="[";
    return(myword);
    break;
    case']': myword->num=30;
    myword->word="]";
    return(myword);
    break;
    case'{': myword->num=31;
    myword->word="{";
    return(myword);
    break;
    case'}': myword->num=32;
    myword->word="}";
    return(myword);
    break;
    case',': myword->num=33;
    myword->word=",";
    return(myword);
    break;
    case':': mgetch();
    if(ci=='='){
    myword->num=18;
    myword->word=":=";
    return(myword);

    }
    myword->num=33;
    myword->word=":";
    return(myword);
    break;
    case';': myword->num=26;
    myword->word=";";
    return(myword);
    break;
    case'>': mgetch();
    if(ci=='='){
    myword->num=24;
    myword->word=">=";
    return(myword);
    }
    back();
    myword->num=23;
    myword->word=">";
    return(myword);
    break;
    case'<': mgetch();
    if(ci=='='){
    myword->num=22;
    myword->word="<=";
    return(myword);
    }
    back();
    myword->num=20;
    myword->word="<";
    return(myword);
    break;

    case'!': mgetch();
    if(ci=='='){
    myword->num=40;
    myword->word="!=";
    return(myword);
    }
    back();
    myword->num=-1;
    myword->word="error";
    return(myword);
    break;

    case'': myword->num=1000;
    myword->word="over";
    return(myword);
    break;
    default:
    myword->num=-1;
    myword->word="error";
    return(myword);

    }

    }

    int main()
    {
    int over=1; WORD * oneword; int i=0;
    oneword=(WORD*)malloc(sizeof(WORD));
    ainput=0;
    printf("输入源代码:");
    do{
    ci=getchar();
    input[i]=ci;
    i++;
    }while(ci!='#');
    while(over<1000&&over!=-1)
    {
    oneword=scaner();
    if(oneword->num<1000)
    printf("(%d,%s) ",oneword->num,oneword->word);
    over=oneword->num;
    }



    }

  • 相关阅读:
    深入理解javascript函数进阶系列第三篇——函数节流和函数防抖
    深入理解javascript函数进阶系列第二篇——函数柯里化
    深入理解javascript函数进阶系列第一篇——高阶函数
    javascript面向对象系列第四篇——OOP中的常见概念
    CSS以图换字的9种方法
    javascript面向对象系列第三篇——实现继承的3种形式
    使用gitbook
    linux云服务器常用设置
    Git要点
    Linux环境下安装NodeJS和mongoDB
  • 原文地址:https://www.cnblogs.com/zlcan/p/4878101.html
Copyright © 2011-2022 走看看