码迷,mamicode.com
首页 > 其他好文 > 详细

Compiler_词法分析_直接转向法

时间:2014-05-09 22:13:26      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:compiler

bubuko.com,布布扣

依据上述转换图,写出直接转向代码:

//============================================================================
// Name        : Comlier.cpp
// Author      : vit
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string.h>

using namespace std;

bool confirm;
int status;
int no;

bool JudgeNum(char &a)
{
    if(a >= ‘0‘ && a <= ‘9‘)
        return true;
    else
        return false;
}


void analysis(string &a)
{
    if(status == 0)
    {
        if(a[no] == ‘.‘)
        {
            status = 6;
            no ++;
            analysis(a);
        }
        else if(JudgeNum(a[no]))
        {
            status = 1;
            no ++;
            analysis(a);
        }
        else
        {
            confirm = false;
//            cout << status;
            return;
        }
    }
    else if(status == 1)
    {
        if(JudgeNum(a[no]) || a[no] == ‘.‘)
        {
            status = 2;
            no++;
            analysis(a);
        }
        else if(a[no] == ‘E‘|| a[no] == ‘e‘)
        {
            status = 5;
            no++;
            analysis(a);
        }
        else
        {
            confirm = false;
//            cout << status;
            return ;
        }
    }
    else if(status == 2)
    {
        if(JudgeNum(a[no]))
        {
            status = 2;
            no++;
            analysis(a);
        }
        else if(a[no] == ‘E‘|| a[no] == ‘e‘)
        {
            status = 3;
            no++;
            analysis(a);
        }
        else
        {
            confirm = true;
//            cout << status;
            return;
        }
    }
    else if(status == 3)
    {
        if(a[no] == ‘+‘ || a[no] == ‘-‘)
        {
            status = 4;
            no++;
            analysis(a);
        }
        else
        {
            confirm = false;
//            cout << status;
            return;
        }
    }
    else if(status == 4)
    {
        if(JudgeNum(a[no]))
        {
            status = 5;
            no++;
            analysis(a);
        }
        else
        {
            confirm = false;
//            cout << status;
            return;
        }
    }
    else if(status == 5)
    {
        if(JudgeNum(a[no]))
        {
            status = 5;
            no++;
            analysis(a);
        }
        else
        {
            confirm = true;
//            cout << status;
            return;
        }
    }
    else if(status == 6)
    {
        if(JudgeNum(a[no]))
        {
            status = 2;
            no++;
            analysis(a);
        }
        else
        {
            confirm = false;
//            cout << status;
            return;
        }
    }
    else
        cout << "status error." << endl;

}


int main()
{
    string a;
    while(cin >> a)
    {
        confirm = false;
        status = 0;
        no = 0;
        analysis(a);
        if(confirm)
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
}


Compiler_词法分析_直接转向法,布布扣,bubuko.com

Compiler_词法分析_直接转向法

标签:compiler

原文地址:http://blog.csdn.net/svitter/article/details/25402579

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!