题目如下,就是要读取一个文档,建立一个有限状态机,然后再读取一个字符表,判断这个字符表里的字符是否能在状态机里面运行,求大神帮忙啊,初学而已,对这个题无从下手
Assignment 1
In this assignment you will write a Java implementation of a deterministic finite state automaton.
You must have separate classes for:
an automaton.
a configuration of an automaton.
your main method.
The alphabet of the automaton will be represented by Java primitive type char, and words (i.e. strings)
over the alphabet, by Java class String. You may have a separate classes for states, or you may
represent states by Java primitive type int.
All your classes must be in a package called clu.edu.automaton.
Your Automaton class must contain:
A static method with signature
public static Automaton readAutomaton(String fileName)
This method must read a file (with the given name) containing a description of an automaton,
M, and returns an instance of your Automaton class representing M.
A non-static method with signature
public Configuration yieldsInOneStep(Configuration c)
This method must return the configuration yielded from the given configuration in one step.
A non-static method with signature
public boolean accepts(String w)
This method must return true/false depending on whether M accepts w or not. This method
must utilize the yieldsInOneStep method.
A non-static method with signature
public void printYields(String w)
This method must print the sequence of configurations that M passes through (including the
initial and final configurations) when M is run with w as input. This method must utilize the
yieldsInOneStep method.
Your main() method must:
Call the readAutomaton method in class Automaton to read an automaton M.
Read a file containing an input string, w.
Call M.accepts and M.printYields methods, printing results if appropriate.
The automaton description file will have the following format:
Line 1: an integer being the number of states.
Line 2: an integer being the initial state.
Line 3: a list of integers separated by commas, being the final states.
Line 4, 5, etc: each line will have a the form q, c, p, where q and p are states, and c is a char ?
so each line defines a state transition from state q to state p when c is the symbol under the tape
head.
The input string file will contain a single line being the input string.
Make sure you comment your code appropriately ?so reasonable, precise, terse comments.

解决方案 »

  1.   


    无符号实数的有穷自动机的实现
    1、无符号数的BNF描述如下:
    0.<无符号数>  d <余留无符号数> | . <十进制数> | e <指数部分>
          1.<余留无符号数>  d <余留无符号数> | . <十进制数> | e <指数部分> | ε
          2.<十进制小数>  d <余留十进制小数>
    3.<余留十进制小数> e <指数部分> | d <余留十进制小数> | ε
    4.<指数部分>  d <余留整指数数> | + <整指数> | - <整指数>
    5.<整指数>  d <余留整指数数>
    6.<余留整指数数>  d <余留整指数数> | ε#include <stdio.h>
    #include <string.h>int isUnsignNum(char *str)
    {
    int i = 0, statu = 0;
    if(str[0] >= '0' && str[0] <= '9')
    {
    if(str[0] == '0' && !(str[1] == '.' || str[1] == '\0'))//0.######
    return 0;
    }
    else
    {
    return 0;
    }
    while(str[++i] != '\0')
    {
    switch(statu)
    {
    case 0:
    if(str[i] == '.')
    statu = 2;
    else if(str[i] >= '0' && str[i] <= '9')
    statu = 1;
    else if(str[i] == 'e')
    statu = 4;
    else
    return 0;
    break;
    case 1:
    if(str[i] == '.')
    statu = 2;
    else if(str[i] >= '0' && str[i] <= '9')
    statu = 1;
    else if(str[i] == 'e')
    statu = 4;
    else
    return 0;
    break;
    case 2:
    if(str[i] >= '0' && str[i] <= '9')
    statu = 3;
    else
    return 0;
    break;
    case 3:
    if(str[i] >= '0' && str[i] <= '9')
    statu = 3;
    else if(str[i] == 'e')
    statu = 4;
    else
    return 0;
    break;
    case 4:
    if(str[i] >= '0' && str[i] <= '9')
    statu = 6;
    else if(str[i] == '+' || str[i] == '-')
    statu = 5;
    else
    return 0;
    break;
    case 5:
    if(str[i] >= '0' && str[i] <= '9')
    statu = 6;
    else
    return 0;
    break;
    case 6:
    if(str[i] >= '0' && str[i] <= '9')
    statu = 6;
    else
    return 0;
    break;
    }
    }
    if(statu == 1 || statu == 3 || statu == 6)
    return 1;
    else
    return 0;
    }int main()
    {
    char str[100] = {0};
    while(1)
    {
    scanf("%s", str);
    getchar();
    printf("---------%d\n", isUnsignNum(str));
    }
    return 0;
    }这个是很久以前学编译原理的时候用C语言写的
    大致就是这么个思路吧
      

  2.   

    没大神帮忙指点吗
    我翻译下题目,大概意思就是读取一个状态机
    这个状态机如三楼图片所示,而且已经转换成以数字表示的一个txt文档(三楼的文档写得有误,我修正一下,如下)
     3
     0
     2
     0, 0, 1
     0, 1, 0
     1, 0, 1
     1, 1, 2
     2, 0, 1
     2, 1, 0
    (第一行代表状态机有几个状态
    第二行代表状态机的初始状态
    第三行代表终止状态
    第四行的三个数分别为 状态,输入数值,跳转的状态
    因此可以理解为第四行代表当在状态q1(在txt文档中用0表示)的时候,输入0,则跳至状态q2(在txt文档中用1表示,同理q3用2表示)
    第五行代表当在状态q1的时候,输入1,则跳至状态q1
    第六行代表当在状态q2的时候,输入0,则跳至状态q2
    以此类推直至结束代表的都是在不同状态输入不同的数值的时候相应的跳转状态是多少

    要用java读取这个txt文档然后实现这个状态机(有不同的txt文档,分别可以实现不同的状态机,文档里面的格式相同,就是有的状态机状态比较多,有的比较少而已)
    然后需要此程序读取一个字符串w,比如11011001
    判断这个字符串是否能被这个状态机所运行,(所能运行的标准就是当字符串读到最后一位的时候,状态机的状态刚好到达终止状态)
    并且题目还要求此程序打印出每读取这个字符串的字符时状态机的结果是什么样
    就比如读取字符串w 11011001,过程显示如下
    (q1,11011001)--(q1,1011001)
                 --(q1,011001)
                 --(q2,11001)
                 --(q3,1001)
                 --(q2,001)
                 --(q2,01)
                 --(q3,1)
    success
    当字符串w为11011000时,显示
    (q1,11011000)--(q1,1011000)
                 --(q1,011000)
                 --(q2,11000)
                 --(q3,1000)
                 --(q2,000)
                 --(q2,00)
                 --(q2,0)
    false