Node * parseOR() { Node * res = parseXOR(); while (lex.getToken() == OR) { lex.next(); res = new Node("OR", res, parseXOR()); } return res; }
Node * parseAND() { Node * res = parseTerm(); while (lex.getToken() == AND) { lex.next(); res = new Node("AND", res, parseTerm()); } return res; }
string TsToCSharp::Translate(string source) { LexicalAnalyzer lexer; auto tokenizedSource = lexer.Tokenize(source, true); auto parseTree = this->parser.Parse(tokenizedSource); stringstream stream; TranslateSubtree(*parseTree, stream, 0); return stream.str(); }
int main(int n_arg, char** args){ //Variáveis e Ponteiros ifstream *code = NULL; ofstream *lexc = NULL; //string l_reserv[] = {"program", "var", "integer","real","boolean", // "procedure","begin","end","if", "then","else","while","do","not"}; char input[256] = "programa.txt", output[256] = "programa.lex"; //strcmp(input, args[1]); //strcmp(output, args[1]); //strcat(input, ".txt"); //strcat(output, ".out"); //std::list<lexToken> list; //Abertura de Arquivos //ISSO AQUI TEM ERRO LAU, CONSERTA AÍ. VOU ASSUMIR QUE CODE e LEXC ESTÃO FUNCIONANDO //O meu intellisense tá dizendo aqui que o operador "+" não pode ser usado com esses operandos aí code = new ifstream(input); if (code == NULL){ cout << "Arquivo invalido" << endl; } lexc = new ofstream(output); //Vou fazer a escrita em C, depois se você quiser mudar beleza. //FILE *input, *output; //input = fopen((char*)(f_name),"r"); //output = fopen((char*)(e_lexc),"w"); //Análise Léxica LexicalAnalyzer *lex = new LexicalAnalyzer(); lex->analyze(code); lex->writeOutput(lexc); //Fechamento de Arquivos code->close(); lexc->close(); //Deletações delete lex; delete lexc; delete code; //Análise Semântica SyntacticAnalyzer *sa = new SyntacticAnalyzer("programa.lex"); sa->analyze(); delete sa; //Retorno return 0; }
Node * parseTerm() { if (lex.getToken() == LPAREN) { lex.next(); Node * tmp = parseOR(); assert(lex.getToken() == RPAREN); lex.next(); return tmp; } if (lex.getToken() == NOT) { lex.next(); Node * tmp = parseTerm(); return new Node("NOT", tmp, NULL); } if (lex.getToken() == VAR) { string v = lex.getVar(); lex.next(); return new Node(v); } assert(false); }
Parser(string s): lex(s) { lex.next(); }
int main () { try { Diagnostic::Stream dout; fs::path file_path ("stdout"); InputStreamAdapter isa (std::cin); CPP::Preprocessor pp (isa); LexicalAnalyzer lexer (pp); TokenList token_stream; //@@ bad token comparison for (TokenPtr token = lexer.next ();; token = lexer.next ()) { token_stream.push_back (token); if (ReferenceCounting::strict_cast<EndOfStream> (token) != 0) break; } if (token_stream.size () < 2) { cerr << "no tokens produced so nothing to parse" << endl; return 0; } TranslationUnit tu; // Compilation context. // CCF::CompilerElements::Context context; context.set ("file-path", file_path); context.set ("trace-semantic-action", false); SemanticAction::Impl::Factory actions (context, dout, tu); Parser parser (context, dout, lexer, actions); //@@ should be able to use IDL3 here. Or better yet get rid of this // function completely. // CCF::IDL2::Parsing::parse (token_stream.begin (), token_stream.end (), parser.start ()); if (dout.error_count () != 0) return -1; IDL3::Generator g; g.generate (tu); } catch (std::bad_cast const&) { cerr << "bad cast exception" << endl; } catch (InvalidName const&) { cerr << "invalid name exception" << endl; } catch (...) { cerr << "caught unknown exception" << endl; return -1; } }