Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define NMAX 1005
- #define pb push_back
- #define ll long long
- #define INF 0x3f3f3f3f
- #define EPS 1e-3
- using namespace std;
- bool is_type(string symbol);
- bool is_identifier(string symbol);
- bool is_constant(string symbol);
- bool is_expression(string symbol);
- bool is_term(string symbol);
- bool is_factor(string symbol);
- bool is_declaration(string symbol);
- int main() {
- char separatori[] = ",{};<>=![]";
- string line;
- int nr_lines = 0;
- ifstream t("fisier.in");
- stringstream buffer;
- buffer << t.rdbuf();
- string code = buffer.str();
- ofstream fout("fisier.out");
- assert(is_identifier("abfes") == true);
- assert(is_identifier("ab__1s") == true);
- assert(is_identifier("a1235") == true);
- assert(is_identifier("1fesv54") == false);
- assert(is_identifier("fes+bgdf") == false);
- assert(is_constant("1245439") == true);
- assert(is_constant("100004") == true);
- assert(is_constant("+0") == true);
- assert(is_constant("-41656") == true);
- assert(is_constant("01312") == false);
- assert(is_constant("+012") == false);
- assert(is_expression("432+654"));
- /*
- ++nr_lines;
- while() {
- string symbol = ""
- while(pos < line.size() && strchr(separatori, line[pos])) {
- symbol.push_back(line[pos]);
- ++pos;
- }
- if(is_identificator(symbol)) {
- }
- if(is_constant())
- while(line[pos] is in sper)
- }
- */
- return 0;
- }
- bool is_type(string symbol) {
- return symbol == "int" && symbol == "bool" && symbol == "char" && symbol == "double";
- }
- bool is_identifier(string symbol) {
- if(isalpha(symbol[0]) == 0)
- return false;
- for(int i =1;i<symbol.size();++i)
- if(isalnum(symbol[i]) == 0 && symbol[i] != '_')
- return false;
- return true;
- }
- bool is_constant(string symbol) {
- int start = 0;
- if(symbol[0] == '+' || symbol[0] == '-')
- start = 1;
- if(symbol[start] == '0' && symbol.size() > 1+start) return false;
- for(int i=start;i<symbol.size();++i)
- if(isdigit(symbol[i]) == 0)
- return false;
- return true;
- }
- bool is_expression(string symbol) {
- if (is_term(symbol))
- return true;
- string expression = "";
- string term = "";
- int i;
- for(i=0;i<symbol.size() && symbol[i] != '+';++i)
- expression.push_back(symbol[i]);
- cout << expression << ' ' << term;
- for(i=i+1;i<symbol.size();++i) // Aici trebuie schimbat
- term.push_back(symbol[i]);
- cout << expression << ' ' << term;
- return is_expression(expression) && is_term(term);
- }
- bool is_term(string symbol) {
- if (is_factor(symbol))
- return true;
- string term = "";
- string factor = "";
- int i;
- for(i=0;i<symbol.size() && symbol[i] != '*';++i)
- term.push_back(symbol[i]);
- for(i=i+1;i<symbol.size();++i) // Aici trebuie schimbat
- factor.push_back(symbol[i]);
- return is_term(term) && is_factor(factor);
- }
- bool is_factor(string symbol) {
- if(is_constant(symbol))
- return true;
- return is_expression(symbol) || is_identifier(symbol); // sa am grija cu parantezele
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement