📄 test_visitor.whl.svn-base
字号:
method=LALR1;lexical_analyzer_file="";lexical_analyzer_class=Lexer;generate_visitor=true;dump_lr_automaton_to_file=true;//generate_verbose_prints=true;dump_grammar_to_file=true;terminal t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15;S -> A1 | A2 | A3 | A4 | A5 | A6 | A7 | A8 | A9 | A10 ;A1 -> t1 t2* ;A2 -> t2 (t2 | t3 | t4)*;A3 -> t3 iteration_pair(t2 t3, t4)*;A4 -> t4 (t2=nothing | t3 | t4)*;A5 -> t5 (t3=x1 t4 t5=x2 | t1=x1 t4 t2=x2)*;A6 -> t6 ([t2] t3 | t4)*;A7 -> t7 (t1 (t2=x)* )*;A8 -> t8=x1 (t1 = a1 [ t2 = a2 t3 = a3 ] | t2 = a2 t3 = a3 );A9 -> t9 ( (t1 | t2) t3 | t4 )* ;A10 -> t10 (t1 | epsilon) ;code_in_cpp_after_all={#define TEST(m) test(m, m + sizeof(m)/sizeof(m[0]))int main(){ int m1[] = { 1, 2, 2, 2 }; TEST(m1); int m2[] = { 2, 2, 3, 2, 3, 4, 2 }; TEST(m2); int m3[] = { 3, 2, 3, 4, 2, 3 }; TEST(m3); int m4[] = { 2, 2, 3, 2, 3, 4, 2 }; TEST(m4); int m5[] = { 5, 3, 4, 5, 1, 4, 2 }; TEST(m5); int m6[] = { 6, 2, 3, 3, 4 }; TEST(m6); int m7[] = { 7, 1, 2, 2, 1, 1, 2 }; TEST(m7); int m8[] = { 8, 2, 3 }; TEST(m8); int m9[] = { 9, 1, 3, 2, 3, 4 }; TEST(m9); int m10[] = { 10 }; TEST(m10); }};code_in_h={#include <vector>class Lexer {public: Lexer() : index(0) {} void add(int terminal) { terminals.push_back(terminal); } template<class InputIterator> void add(InputIterator b, InputIterator e) { terminals.insert(terminals.end(), b, e); } Whale::Terminal* get_token() { if (index < terminals.size()) return new Terminal(terminals[index++]+1, index); else return new Terminal(0, index); } private: class Terminal : public Whale::Terminal { public: Terminal(int n, int l) : n(n) { line = l; } int number() const { return n; } private: int n; }; int index; std::vector<int> terminals;};};code_in_cpp={#include <iostream>#include <functional>#include <typeinfo>#include <algorithm>using namespace std;class Line_number_and_terminal_visitor : public Whale::Visitor {public: std::vector<int> lines; std::vector<int> terminals; void visit(const Whale::Terminal& t) { terminals.push_back(t.number()-1); lines.push_back(t.line); } };template<class InputIterator>void test(InputIterator b, InputIterator e){ vector<int> input(b, e); Lexer lexer; lexer.add(input.begin(), input.end()); WhaleParser parser(lexer); Whale::NonterminalS* s = parser.parse(); if (!s) { cerr << "Error\n"; } else { Line_number_and_terminal_visitor v; s->accept(v); for (size_t i = 0; i < v.lines.size(); ++i) cout << v.lines[i] << "\n"; if (adjacent_find(v.lines.begin(), v.lines.end(), greater<int>()) != v.lines.end()) { cerr << "error detected.\n"; } if (v.terminals != input) { cerr << "Incorrect list of visited terminals.\n"; cerr << "error detected.\n"; } }}};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -