some functions of PDA in parser updated.
This commit is contained in:
parent
b4f86ba667
commit
62e1837704
|
@ -83,6 +83,8 @@
|
|||
|
||||
//statement
|
||||
<func_return> ::= <return> <scalar>
|
||||
<statement> ::= <definition>|<assignment>|<loop>|<choose>|<use_function>|<func_return> <;>
|
||||
<loop_continue> ::= <continue>
|
||||
<loop_break> ::= <break>
|
||||
<statement> ::= <definition>|<assignment>|<loop>|<choose>|<use_function>|<func_return>|<loop_continue>|<loop_break> <;>
|
||||
<statements> ::= <statement> <statement>
|
||||
<statements> ::= <statements> <statement>
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <thread>
|
||||
#include <ctime>
|
||||
#include <cmath>
|
||||
#include <stack>
|
||||
|
||||
namespace nasal
|
||||
{
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include "nasal.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string command;
|
||||
std::cout<<">> nasal-- script by ValKmjolnir"<<std::endl;
|
||||
std::cout<<">> input \"help\" to find help."<<std::endl;
|
||||
while(1)
|
||||
{
|
||||
|
@ -13,13 +11,15 @@ int main()
|
|||
std::getline(std::cin,command);
|
||||
if(command=="help")
|
||||
{
|
||||
std::cout<<">> 1. input file name to run the nasal script."<<std::endl;
|
||||
std::cout<<">> 2. command cls to clear the screen."<<std::endl;
|
||||
std::cout<<">> 3. command exit to shut down the program."<<std::endl;
|
||||
std::cout<<">> 4. command lexer to see tokens in stack."<<std::endl;
|
||||
std::cout<<">> 5. command del to delete all elements in stack."<<std::endl;
|
||||
std::cout<<">> 6. command run to run the programme in stack."<<std::endl;
|
||||
std::cout<<">> 7. command rs to check the source program."<<std::endl;
|
||||
std::cout<<">> nasal-- script by ValKmjolnir"<<std::endl;
|
||||
std::cout<<">> 1. input file name to run the lexer."<<std::endl;
|
||||
std::cout<<">> 2. command \"cls\" to clear the screen."<<std::endl;
|
||||
std::cout<<">> 3. command \"exit\" to shut down the program."<<std::endl;
|
||||
std::cout<<">> 4. command \"lexer\" to see tokens in stack."<<std::endl;
|
||||
std::cout<<">> 5. command \"parser\" to run parser."<<std::endl;
|
||||
std::cout<<">> 6. command \"del\" to delete all elements in stack."<<std::endl;
|
||||
std::cout<<">> 7. command \"run\" to run the programme in stack."<<std::endl;
|
||||
std::cout<<">> 8. command \"rs\" to check the source program."<<std::endl;
|
||||
}
|
||||
else if(command=="cls")
|
||||
{
|
||||
|
@ -41,10 +41,11 @@ int main()
|
|||
nasal::nasal_var_stack.delete_all();
|
||||
nasal::nasal_func_stack.delete_all();
|
||||
}
|
||||
else if(command=="parser")
|
||||
nasal::nasal_parse.parse_work(nasal::nasal_lexer);
|
||||
else if(command=="run")
|
||||
{
|
||||
nasal::nasal_var_stack.delete_all();
|
||||
nasal::nasal_func_stack.delete_all();
|
||||
;
|
||||
}
|
||||
else
|
||||
nasal::RunProcess(command);
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
#ifndef __NASAL_LEXER_H__
|
||||
#define __NASAL_LEXER_H__
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cstring>
|
||||
#include "nasal_functional.h"
|
||||
|
||||
#define FAIL -1 //ʧ°Ü
|
||||
|
|
|
@ -1,55 +1,97 @@
|
|||
#ifndef __NASAL_PARSE_H__
|
||||
#define __NASAL_PARSE_H__
|
||||
#include "nasal_lexer.h"
|
||||
#include "nasal_token.h"
|
||||
|
||||
enum token_type
|
||||
{
|
||||
__stack_end,__identifier,__use_identifier,
|
||||
__scalar,__function,__var,__equal,__semi,__definition
|
||||
};
|
||||
token_type token_type_tbl;
|
||||
|
||||
struct parse_unit
|
||||
{
|
||||
int type;
|
||||
std::string content;
|
||||
int line;
|
||||
};
|
||||
|
||||
class parse
|
||||
{
|
||||
private:
|
||||
parse_unit *content_array;
|
||||
parse_unit *statement;
|
||||
int len;
|
||||
public:
|
||||
parse();
|
||||
~parse();
|
||||
void content_array_set_empty();
|
||||
void statement_set_empty();
|
||||
std::stack<parse_unit> parser;
|
||||
public:
|
||||
void definition_reduction();
|
||||
void parse_work(token_list&);
|
||||
};
|
||||
|
||||
parse::parse()
|
||||
void parse::definition_reduction()
|
||||
{
|
||||
len=0;
|
||||
content_array=new parse_unit[4096];
|
||||
statement=new parse_unit[1024];
|
||||
}
|
||||
parse::~parse()
|
||||
{
|
||||
if(content_array)
|
||||
delete []content_array;
|
||||
if(statement)
|
||||
delete []statement;
|
||||
}
|
||||
void parse::content_array_set_empty()
|
||||
{
|
||||
for(int i=0;i<4096;++i)
|
||||
int tbl[5];
|
||||
std::stack<parse_unit> temp;
|
||||
for(int i=4;i>=0;--i)
|
||||
{
|
||||
content_array[i].line=0;
|
||||
content_array[i].type=0;
|
||||
if(parser.empty())
|
||||
break;
|
||||
temp.push(parser.top());
|
||||
tbl[i]=temp.top().type;
|
||||
parser.pop();
|
||||
}
|
||||
if(tbl[0]==__var && tbl[1]==__identifier && tbl[2]==__equal && tbl[3]==__scalar && tbl[4]==__semi)
|
||||
{
|
||||
parse_unit temp_parse_unit;
|
||||
temp_parse_unit.type=__definition;
|
||||
temp_parse_unit.line=temp.top().line;
|
||||
parser.push(temp_parse_unit);
|
||||
}
|
||||
else
|
||||
for(int i=0;i<5;++i)
|
||||
{
|
||||
if(temp.empty())
|
||||
break;
|
||||
parser.push(temp.top());
|
||||
temp.pop();
|
||||
}
|
||||
return;
|
||||
}
|
||||
void parse::statement_set_empty()
|
||||
|
||||
void parse::parse_work(token_list& lexer)
|
||||
{
|
||||
for(int i=0;i<1024;++i)
|
||||
parse_unit temp_parse;
|
||||
token_unit *temp=lexer.get_head();
|
||||
while(temp->next)
|
||||
{
|
||||
statement[i].line=0;
|
||||
statement[i].type=0;
|
||||
temp=temp->next;
|
||||
temp_parse.line=temp->line;
|
||||
if(temp->content=="var")
|
||||
{
|
||||
token_type_tbl=__var;
|
||||
temp_parse.type=token_type_tbl;
|
||||
}
|
||||
else if(temp->content=="=")
|
||||
{
|
||||
token_type_tbl=__equal;
|
||||
temp_parse.type=token_type_tbl;
|
||||
}
|
||||
else if(temp->type==IDENTIFIER)
|
||||
{
|
||||
token_type_tbl=__identifier;
|
||||
temp_parse.type=token_type_tbl;
|
||||
}
|
||||
else if(temp->content==";")
|
||||
{
|
||||
token_type_tbl=__semi;
|
||||
temp_parse.type=token_type_tbl;
|
||||
}
|
||||
else if(temp->type==NUMBER || temp->type==STRING)
|
||||
{
|
||||
token_type_tbl=__scalar;
|
||||
temp_parse.type=token_type_tbl;
|
||||
}
|
||||
parser.push(temp_parse);
|
||||
definition_reduction();
|
||||
if(parser.top().type==__definition)
|
||||
std::cout<<"Definition in "<<parser.top().line<<std::endl;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
#ifndef __NASAL_PRINT_H__
|
||||
#define __NASAL_PRINT_H__
|
||||
|
||||
#include<iostream>
|
||||
#include<cstring>
|
||||
#include "nasal_hash.cpp"
|
||||
#include "nasal_list.cpp"
|
||||
|
||||
|
|
Loading…
Reference in New Issue