#include <iostream>
#include <string>
using namespace std;
#include <scan.h>
const char* names[] = {"read", "write", "id", "literal", "gets",
"add", "sub", "mul", "div", "lparen", "rparen", "eof"};
static token input_token;
void error () {
cout<<"syntax error\n"<<endl;
exit (1);
}
void match (token expected) {
if (input_token == expected) {
cout<<"matched ", names[input_token]<<endl;
if (input_token == t_id || input_token == t_literal)
cout<<": %s", token_image<<endl;
cout"\n"<<endl;
input_token = scan ();
}
else error ();
}
void program ();
void stmt_list ();
void stmt ();
void expr ();
void term ();
void term_tail ();
void factor ();
void factor_tail ();
void add_op ();
void mul_op ();
void program () {
switch (input_token) {
case t_id:
case t_read:
case t_write:
case t_eof:
cout<<"predict program --> stmt_list eof\n"<<endl;
stmt_list ();
match (t_eof);
break;
default: error ();
}
}
void stmt_list () {
switch (input_token) {
case t_id:
case t_read:
case t_write:
cout<<"predict stmt_list --> stmt stmt_list\n";
stmt ();
stmt_list ();
break;
case t_eof:
cout<<"predict stmt_list --> epsilon\n";
break; /* epsilon production */
default: error ();
}
}
void stmt () {
switch (input_token) {
case t_id:
cout<<"predict stmt --> id gets expr\n";
match (t_id);
match (t_gets);
expr ();
break;
case t_read:
cout<<"predict stmt --> read id\n";
match (t_read);
match (t_id);
break;
case t_write:
cout<<"predict stmt --> write expr\n";
match (t_write);
expr ();
break;
default: error ();
}
}
void expr () {
switch (input_token) {
case t_id:
case t_literal:
case t_lparen:
cout<<"predict expr --> term term_tail\n";
term ();
term_tail ();
break;
default: error ();
}
}
void term () {
switch (input_token) {
case t_id:
case t_literal:
case t_lparen:
cout<<"predict term --> factor factor_tail\n";
factor ();
factor_tail ();
break;
default: error ();
}
}
void term_tail () {
switch (input_token) {
case t_add:
case t_sub:
cout<<"predict term_tail --> add_op term term_tail\n";
add_op ();
term ();
term_tail ();
break;
case t_rparen:
case t_id:
case t_read:
case t_write:
case t_eof:
cout<<"predict term_tail --> epsilon\n";
break; /* epsilon production */
default: error ();
}
}
void factor () {
switch (input_token) {
case t_literal:
cout<<"predict factor --> literal\n";
match (t_literal);
break;
case t_id :
cout<<"predict factor --> id\n";
match (t_id);
break;
case t_lparen:
cout<<"predict factor --> lparen expr rparen\n";
match (t_lparen);
expr ();
match (t_rparen);
break;
default: error ();
}
}
void factor_tail () {
switch (input_token) {
case t_mul:
case t_div:
cout<<"predict factor_tail --> mul_op factor factor_tail\n";
mul_op ();
factor ();
factor_tail ();
break;
case t_add:
case t_sub:
case t_rparen:
case t_id:
case t_read:
case t_write:
case t_eof:
cout<<"predict factor_tail --> epsilon\n";
break; /* epsilon production */
default: error ();
}
}
void add_op () {
switch (input_token) {
case t_add:
cout<<"predict add_op --> add\n";
match (t_add);
break;
case t_sub:
cout<<"predict add_op --> sub\n";
match (t_sub);
break;
default: error ();
}
}
void mul_op () {
switch (input_token) {
case t_mul:
cout<<"predict mul_op --> mul\n";
match (t_mul);
break;
case t_div:
cout<<"predict mul_op --> div\n";
match (t_div);
break;
default: error ();
}
}
int main () {
input_token = cin();
program ();
return 0;
}
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}