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