47int Recognizer::nextToken()
58 int token = ch.unicode ();
64 while (!ch.isNull () && ch != u'"')
78 qerr() << _M_input_file <<
":" << _M_line <<
": Warning. Expected `\"'" << Qt::endl;
80 _M_current_value = text;
81 return (token = STRING_LITERAL);
84 else if (ch.isLetterOrNumber () || ch == u'_')
87 do { text += ch; inp (); }
88 while (ch.isLetterOrNumber () || ch == u'_' || ch == u'.');
89 _M_current_value = text;
93 else if (token ==
'%')
98 while (ch.isSpace ());
100 do { text += ch; inp (); }
101 while (ch.isLetterOrNumber () || ch == u'_' || ch == u'-');
103 if (text ==
"token_prefix"_L1)
104 return (token = TOKEN_PREFIX);
105 else if (text ==
"merged_output"_L1)
106 return (token = MERGED_OUTPUT);
107 else if (text ==
"token"_L1)
108 return (token = TOKEN);
109 else if (text ==
"start"_L1)
110 return (token = START);
111 else if (text ==
"parser"_L1)
112 return (token = PARSER);
113 else if (text ==
"decl"_L1)
114 return (token = DECL_FILE);
115 else if (text ==
"impl"_L1)
116 return (token = IMPL_FILE);
117 else if (text ==
"expect"_L1)
118 return (token = EXPECT);
119 else if (text ==
"expect-rr"_L1)
120 return (token = EXPECT_RR);
121 else if (text ==
"left"_L1)
122 return (token = LEFT);
123 else if (text ==
"right"_L1)
124 return (token = RIGHT);
125 else if (text ==
"nonassoc"_L1)
126 return (token = NONASSOC);
127 else if (text ==
"prec"_L1)
128 return (token = PREC);
131 qerr() << _M_input_file <<
":" << _M_line <<
": Unknown keyword `" << text <<
"'" << Qt::endl;
133 return (token = ERROR);
139 if (token ==
'-' && ch == u'-')
142 while (!ch.isNull () && ch != u'\n');
146 else if (token ==
':' && ch == u':')
150 return (token = ERROR);
152 return (token = COLON);
155 else if (token ==
'/' && ch == u':')
157 _M_action_line = _M_line;
161 text +=
"\n#line "_L1 + QString::number(_M_action_line) +
162 " \""_L1 + QDir::fromNativeSeparators(_M_input_file) +
"\"\n"_L1;
167 while (! ch.isNull ())
169 token = ch.unicode ();
172 if (token ==
':' && ch == u'/')
175 text += QLatin1Char (token);
179 return (token = ERROR);
183 if (ch.isNull () || ch.isSpace ())
185 _M_current_value = text;
186 return (token = DECL);
193 else if (token ==
'/' && ch == u'.')
195 _M_action_line = _M_line;
199 text +=
"\n#line "_L1 + QString::number(_M_action_line) +
200 " \""_L1 + QDir::fromNativeSeparators(_M_input_file) +
"\"\n"_L1;
206 while (! ch.isNull ())
208 token = ch.unicode ();
211 if (token ==
'.' && ch == u'/')
214 text += QLatin1Char (token);
218 return (token = ERROR);
222 if (ch.isNull () || ch.isSpace ())
224 _M_current_value = text;
225 return (token = IMPL);
234 return (token = COLON);
237 return (token = SEMICOLON);
249bool Recognizer::parse (
const QString &input_file)
251 _M_input_file = input_file;
253 QFile file(_M_input_file);
254 if (! file.open(QFile::ReadOnly))
256 qerr() <<
"qlalr: no input file\n";
260 QString _M_contents = QTextStream(&file).readAll();
261 _M_firstChar = _M_contents.constBegin();
262 _M_lastChar = _M_contents.constEnd();
263 _M_currentChar = _M_firstChar;
271 _M_current_rule = _M_grammar
->rules.end ();
276 state_stack[++tos] = 0;
280 if (yytoken == -1 && - TERMINAL_COUNT != action_index [state_stack [tos]])
281 yytoken = nextToken();
283 int act = t_action (state_stack [tos], yytoken);
285 if (act == ACCEPT_STATE)
290 if (++tos == stack_size)
293 sym_stack [tos] = _M_current_value;
294 state_stack [tos] = act;
303 act = state_stack [tos++];
314 _M_grammar->table_name = sym(2);
318 _M_grammar->merged_output = sym(2);
322 _M_grammar->decl_file_name = sym(2);
326 _M_grammar->impl_file_name = sym(2);
330 _M_grammar->expected_shift_reduce = sym(2).toInt();
334 _M_grammar->expected_reduce_reduce = sym(2).toInt();
338 _M_grammar->token_prefix = sym(2);
343 _M_grammar->spells.insert (name, sym(2));
368 _M_grammar->token_info.insert (name, info);
372 _M_decls += expand (sym(1));
376 _M_impls += expand (sym(1));
381 _M_current_rule->lhs = _M_grammar
->intern (sym(1));
386 qerr() << _M_input_file <<
":" << _M_line <<
": Invalid non terminal `" << *_M_current_rule->lhs <<
"'" << Qt::endl;
394 Name lhs = _M_current_rule->lhs;
396 _M_current_rule->lhs = lhs;
401 qerr() << _M_input_file <<
":" << _M_line <<
": Invalid non terminal `" << *_M_current_rule->lhs <<
"'" << Qt::endl;
409 _M_current_rule->prec = _M_grammar->names.end ();
411 for (NameList::iterator it = _M_current_rule->rhs.begin (); it != _M_current_rule->rhs.end (); ++it)
416 _M_current_rule->prec = *it;
424 qerr() << _M_input_file <<
":" << _M_line <<
": `" << *tok <<
" is not a terminal symbol" << Qt::endl;
425 _M_current_rule->prec = _M_grammar->names.end ();
428 _M_current_rule->prec = tok;
437 _M_current_rule->rhs.push_back (name);
446 state_stack [tos] = nt_action (act, lhs [r] - TERMINAL_COUNT);
455 qerr() << _M_input_file <<
":" << _M_line <<
": Syntax error" << Qt::endl;