CPP   64

mlang cpp

Guest on 20th June 2022 06:15:48 AM

  1. #pragma hdrstop
  2. #pragma argsused
  3. #include <stdio.h>
  4. #include <fstream>
  5. #include <iostream>
  6. #include <sstream>
  7. #include <vector>
  8. #include <string>
  9. #include <stdlib.h>
  10. using namespace std;
  11.  
  12. //lexical_analizator
  13. ifstream mprog;
  14. int k=0,q=1,z=0;
  15. char c;
  16.  
  17. //not a type - type 0
  18. //reserved words - type 1
  19. //delimiters - type 2
  20. //numerical constants - type 3
  21. //identificators - type 4
  22. //string constants - type 5
  23.  
  24. string reserv[] ={"program", "int", "boolean", "true", "false", "if", "while", "read", "write", "case", "of", "end", "break", "not", "and", "or", "else"};
  25. //1 - reserved words             0                      1                       2                    3                   4                  5              6                7                8                9            10           11              12               13            14          15            16
  26.  
  27. string delims[] ={"(", ")", "{", "}", "-", "+", "=", "@", "@", ";", ",", "<", ">", "/", "*", "!=", "<=", ">=", "==", ":"};  //@ - empty symbol
  28. //2 - delimiters                         0      1          2         3          4       5          6         7          8       9        10          11                12         13      14           15        16           17        18          19
  29.  
  30. class lexem
  31. {
  32.  public:
  33.   int type;
  34.   int number;
  35.   string id;
  36.  
  37.   lexem (int type, int number): type(type), number(number) {}
  38.   lexem (int type, string id): type(type), id(id) {}
  39.  
  40.  void print ()
  41.    {
  42.     cout<<type<<"  ";
  43.     if ((type!=4)&&(type!=5)) cout<<number;
  44.     if ((type==4)||(type==5)) cout<<id<<endl;
  45.    }
  46. };
  47.  
  48.  
  49. lexem* L;
  50.  
  51.  
  52. void open_file (int argc, char** argv)
  53. {
  54.  string name,path;
  55.   if (argc<2) {cout<<" ERROR: not enough arguments."<<endl; exit(1); }
  56.   if (argc>2) {cout<<" ERROR: too many arguments."<<endl; exit(1); }
  57.  path=argv[1];
  58.  mprog.open(path.c_str(), ios::in);
  59.   if (!mprog.is_open()){ cout << " ERROR: Wrong directory or wrong filename"<<endl; exit(1);}
  60.  return;
  61. }
  62.  
  63. //------------------------------------------------ LEXICAL ANALYSIS ------------------------------------------------
  64.  
  65. lexem* getlex ()
  66.  
  67. {
  68.  string s;
  69.  string buf;
  70.  int type=0,number,d=0,comm=0;
  71.  stringstream value (stringstream::in | stringstream::out);
  72.  
  73. type=0;
  74. if (k!=1) mprog.get(c);
  75. k=0;
  76.  
  77.  while (mprog.good())
  78.  {
  79.   if (comm) // catching comments
  80.    while (mprog.good())
  81.    {
  82.     mprog.get(c);
  83.     if (c=='*')
  84.      {
  85.       mprog.get(c);
  86.       if (c=='/')
  87.        {
  88.         comm=0;
  89.         buf.clear();
  90.         mprog.get(c);
  91.         type=0;
  92.         break;
  93.        }
  94.      }
  95.    }
  96.  
  97. //  if (comm) cout<<"X"<<c<<"X";
  98.  
  99.  
  100.   while ((isspace(c)) && (type==0))
  101.   {
  102.    mprog.get(c);
  103.    if (!mprog.good()) {q=0; L->type=0; return L;}
  104.   }
  105.   d=0;
  106.  
  107. //   if (!mprog.good()) q=0;
  108.  
  109.   if ((isspace(c)) && (type!=0)) d=1;
  110.   else
  111.  {
  112.    if ((isupper(c))||(islower(c)))
  113.     {
  114.      if (type==0) type=4; //id or reserved word
  115.      if (type==3) { cout<<" LEXICAL ERROR: Incorrect numerical constant format"<<endl; exit(1);}
  116.     }
  117.    else if (isdigit(c))
  118.     {
  119.      if (type==0) type=3; //numerical constant
  120.     }
  121.    else if  ((c==',')||(c==';')||(c=='(')||(c==')')||(c=='{')||(c=='}')||(c=='-')||(c=='+')||(c=='=')||(c=='<')||(c=='>')||(c=='!')||(c=='*')||(c=='/')||(c==':'))
  122.     {
  123.      if (type!=0)
  124.       {
  125.        d=1;
  126.        k=1;
  127.       }
  128.      else
  129.       {
  130.        type=2; d=1;
  131.        buf=buf+c;
  132.        if (c==':') number=19;
  133.        if (c=='(') number=0;           if (c==')') number=1;       if (c=='{') number=2;
  134.        if (c=='}') number=3;           if (c=='-') number=4;       if (c=='+') number=5;
  135.        if (c==';') number=9;           if (c==',') number=10;      if (c=='*') number=14;
  136.        if (c=='!') {mprog.get(c); if (c=='=') {buf=buf+c;number=15;} else { cout<<" LEXICAL ERROR: Wrong symbol \""<<c<<"\" "<<endl; exit(1);}} else
  137.        if (c=='<') {mprog.get(c); if (c=='=') {buf=buf+c; number=16;} else {number=11; k=1;} } else
  138.        if (c=='>') {mprog.get(c); if (c=='=') {buf=buf+c; number=17;} else {number=12; k=1;} } else
  139.        if (c=='=') {mprog.get(c); if (c=='=') {buf=buf+c; number=18;} else {number=6; k=1;} } else
  140.        if (c=='/') {mprog.get(c); if (c=='*') {comm=1; d=0;} else {number=13; k=1;} }
  141.       }
  142.     }
  143.    else if (c=='\"')
  144.     {
  145.      type=5;
  146.      d=1;
  147.      while (mprog.good())
  148.       {
  149.        mprog.get(c);
  150.        if (c!='\"') buf=buf+c;
  151.        else break;
  152.       }
  153.     }
  154.    else { cout<<" LEXICAL ERROR: Wrong symbol \""<<c<<"\" "<<endl; exit(1);}
  155.  }
  156.   if ((type==4) && (d)) for(int i=0; i<17;i++) if (buf==reserv[i]) {type=1; number=i;}
  157.   if ((d)&&((type==1)||(type==2))) L=new lexem(type,number);
  158.   if ((d)&&(type==3)) {value<<buf; value>>number; L=new lexem(type,number);}
  159.   if ((d)&&((type==4)||(type==5))) L=new lexem(type,buf);
  160.   if (!d) buf=buf+c;
  161.   if (d) {/*cout<<"#"<<buf<<"#"<<endl; */buf.clear(); type=0; d=0; break;}
  162.  
  163.   if (k!=1) mprog.get(c);
  164.   k=0;
  165.  }
  166.  if (!mprog.good()) {q=0; L->type=0;}
  167. return L;
  168. }
  169.  
  170.  
  171.  
  172. //------------------------------------------------ SYNTAX ANALYSIS -------------------------------------------------
  173. void IF();
  174. void WHILE ();
  175. void READ ();
  176. void WRITE ();
  177. void CASE ();
  178. void OPERATOR ();
  179. void EXPRESSION ();
  180. void OR ();
  181. void AND ();
  182. void COMPARE ();
  183. void ADD_SUB ();
  184. void MULT_DIV ();
  185. void VALUE ();
  186.  
  187.  
  188.  
  189.  
  190. void ERROR (string s)
  191. {
  192.  cout<<endl<<s<<endl;
  193.  getchar();
  194.  exit(1);
  195. }
  196.  
  197.  
  198. void VARIABLE ()
  199. {
  200.  if (L->type!=4) ERROR (" SYNTAX ERROR: identificator missing in a variable description");
  201.  L=getlex();
  202.  if ((L->type==2)&&(L->number==6))
  203.   {
  204.    L=getlex();
  205.    if ((L->type==3)||((L->type==1)&&((L->number==3)||(L->number==4)))) L=getlex();
  206.    else  ERROR (" SYNTAX ERROR: wrong constant type in a variable description");
  207.   }
  208. }
  209.  
  210.  
  211. void DESCRIPTION ()
  212. {
  213.  VARIABLE ();
  214.  while ((L->type==2)&&(L->number==10))
  215.   {
  216.    L=getlex();
  217.   //   cout<<L->type<<"-"<<L->number<<endl;
  218.    VARIABLE ();
  219.    if (((L->type!=2)||(L->number!=10))&&((L->type!=2)||(L->number!=9))) ERROR (" SYNTAX ERROR: wrong delimiter between variable descriptions");
  220.   }
  221.  
  222.  if ((L->type!=2)||(L->number!=9)) ERROR (" SYNTAX ERROR: wrong delimiter in the end of variable descriptions");
  223.     L=getlex();
  224.  }
  225.  
  226.  
  227. void EQ ()
  228. {
  229.  EXPRESSION();
  230.   if ((L->type==2)&&(L->number==6))
  231.   {
  232.    L=getlex();
  233.    EQ();
  234.   }
  235. }
  236.  
  237.  
  238. void EXPRESSION ()
  239. {
  240.  OR();
  241.  while ((L->type==1)&&(L->number==15))
  242.   {
  243.    L=getlex();
  244.    OR();
  245.   }
  246. }
  247.  
  248. void OR ()
  249. {
  250.  AND();
  251.  while ((L->type==1)&&(L->number==14))
  252.   {
  253.    L=getlex();
  254.    AND();
  255.   }
  256. }
  257.  
  258.  
  259. void AND ()
  260. {
  261.  COMPARE();
  262.  while ((L->type==2)&&((L->number==11)||(L->number==12)||(L->number==15)||(L->number==16)||(L->number==17)||(L->number==18)))
  263.   {
  264.    L=getlex();
  265.    COMPARE();
  266.   }
  267. }
  268.  
  269.  
  270. void COMPARE ()
  271. {
  272.  ADD_SUB();
  273.  while ((L->type==2)&&((L->number==4)||(L->number==5)))
  274.   {
  275.    L=getlex();
  276.    ADD_SUB();
  277.   }
  278. }
  279.  
  280.  
  281. void ADD_SUB ()
  282. {
  283.  MULT_DIV();
  284.  while ((L->type==2)&&((L->number==13)||(L->number==14)))
  285.   {
  286.    L=getlex();
  287.    MULT_DIV();
  288.   }
  289. }
  290.  
  291.  
  292. void MULT_DIV ()
  293. {
  294.  VALUE();
  295.  while ((L->type==2)&&((L->number==4)||(L->number==5)))
  296.   {
  297.    L=getlex();
  298.    VALUE();
  299.   }
  300. }
  301.  
  302.  
  303.  void  VALUE()
  304.  {
  305.  if ((L->type==2)&&(L->number==0))
  306.   {
  307.    L=getlex();
  308.    EQ();
  309.    if ((L->type!=2)||(L->number!=1)) ERROR (" SYNTAX ERROR: missing \')\' in operator");
  310.    L=getlex();
  311.   }
  312.  else if (L->type==4) L=getlex();            //identificator
  313.  else if (((L->type==1)&&((L->number==3)||(L->number==4)))||(L->type==3)) L=getlex();    //true, false or numerical constant
  314.  else ERROR(" SYNTAX ERROR: wrong lexem in operator");
  315.       cout<<L->type<<"!"<<L->number<<endl;
  316.  }
  317.  
  318.  
  319.  
  320. void OPERATOR ()
  321. {
  322.  if ((L->type==2)&&(L->number==2))
  323.   {
  324.    L=getlex();
  325.    while ((L->type!=2)||(L->number!=3))
  326.     {
  327.      OPERATOR();
  328.     }
  329.    L=getlex();
  330.   }
  331.   else if((L->type==2)&&(L->number==3)) {return;}
  332.   else
  333.   {
  334.    if ((L->type==1)&&(L->number==5)) {L=getlex(); IF();}
  335.    else if ((L->type==1)&&(L->number==6)) {L=getlex(); WHILE();}
  336.    else if ((L->type==1)&&(L->number==7)) {L=getlex(); READ();}
  337.    else if ((L->type==1)&&(L->number==8)) {L=getlex(); WRITE();}
  338.    else if ((L->type==1)&&(L->number==9)) {L=getlex(); CASE();}
  339.    else if ((L->type==1)&&(L->number==12))
  340.     {
  341.      L=getlex();
  342.      if ((L->type!=2)||(L->number!=9)) ERROR (" SYNTAX ERROR: no \";\" at the end of \"break\"-operator");
  343.      L=getlex();
  344.     }
  345.    else
  346.     {
  347.      EQ();
  348.      if ((L->type!=2)||(L->number!=9)) ERROR(" SYNTAX ERROR: no \";\" at the end of operator");
  349.      L=getlex();
  350.     }
  351.   }
  352. }
  353.  
  354.  
  355.  
  356. void IF()
  357. {
  358.  if ((L->type!=2)||(L->number!=0)) ERROR (" SYNTAX ERROR: missing \'(\' in \"if\"-comparison");
  359.  L=getlex();
  360.  EQ();
  361.  if ((L->type!=2)||(L->number!=1)) ERROR (" SYNTAX ERROR: missing \')\' in \"if\"-comparison");
  362.  
  363.  L=getlex();
  364.  OPERATOR ();
  365.     cout<<L->type<<"*"<<L->number<<endl;
  366.  
  367.  if ((L->type!=1)||(L->number!=16)) ERROR (" SYNTAX ERROR: missing \"else\" in \"if\"-comparison");
  368.  L=getlex();
  369.      cout<<L->type<<"^"<<L->number<<endl;
  370.  OPERATOR ();
  371. }
  372.  
  373.  
  374. void WHILE ()
  375. {
  376.  if ((L->type!=2)||(L->number!=0)) ERROR (" SYNTAX ERROR: missing \'(\' in \"while\"-loop");
  377.  L=getlex();
  378.  EQ();
  379.  if ((L->type!=2)||(L->number!=1)) ERROR (" SYNTAX ERROR: missing \')\' in \"while\"-loop");
  380.  L=getlex();
  381.  if((L->type==2)&&(L->number==3)) ERROR (" SYNTAX ERROR: missing operator in \"while\"-loop");;
  382.  OPERATOR ();
  383. }
  384.  
  385.  
  386. void READ ()
  387. {
  388.  if ((L->type!=2)||(L->number!=0)) ERROR (" SYNTAX ERROR: missing \'(\' in \"read\"-operation");
  389.  L=getlex();
  390.  
  391.  if (L->type!=4) ERROR (" SYNTAX ERROR: missing an identificator for a \"read\"-operation");
  392.  
  393.  L=getlex();
  394.  if ((L->type!=2)||(L->number!=1)) ERROR (" SYNTAX ERROR: missing \')\' in \"read\"-operation");
  395.  L=getlex();
  396.  if ((L->type!=2)||(L->number!=9)) ERROR(" SYNTAX ERROR: no \";\" at the end of operator");
  397.  L=getlex();
  398. }
  399.  
  400. void WRITE ()
  401. {
  402.  if ((L->type!=2)||(L->number!=0)) ERROR (" SYNTAX ERROR: missing \'(\' in \"write\"-operation");
  403.  L=getlex();
  404.  
  405.  if (L->type!=5) EQ (); else L=getlex();
  406.  while ((L->type==2)&&(L->number==10))
  407.   {
  408.    L=getlex();
  409.    if (L->type!=5) EQ (); else L=getlex();
  410.   }
  411.  if ((L->type!=2)||(L->number!=1)) ERROR (" SYNTAX ERROR: missing \')\' in \"write\"-operation");
  412.  L=getlex();
  413.  if ((L->type!=2)||(L->number!=9)) ERROR (" SYNTAX ERROR: no \";\" at the end of \"write\"-operation");
  414.  L=getlex();
  415. }
  416.  
  417. void ONE_CASE ()
  418. {
  419.  while ((L->type!=2)||(L->number!=19))
  420.  {
  421.   if (L->type!=3) ERROR (" SYNTAX ERROR: not a numerical value as a \"case\"-constant");
  422.   L=getlex();
  423.   if ((L->type!=2)||(L->number!=19))
  424.    {
  425.     if ((L->type!=2)||(L->number!=10)) ERROR (" SYNTAX ERROR: wrong delimeter between \"case\" elements");
  426.     L=getlex();
  427.    }
  428.  }
  429.  L=getlex();
  430.  OPERATOR();
  431. }
  432.  
  433. void LIST_OF_CASES ()
  434. {
  435. while ((L->type!=1)||(L->number!=11))
  436.  {
  437.  cout<<L->type<<"#"<<L->number<<endl;
  438.   ONE_CASE ();
  439.  }
  440. }
  441.  
  442.  
  443. void CASE ()
  444. {
  445.  if ((L->type!=2)||(L->number!=0)) ERROR (" SYNTAX ERROR: missing \'(\' in \"case\"-comparison");
  446.  L=getlex();
  447.  EQ ();
  448.  if ((L->type!=2)||(L->number!=1)) ERROR (" SYNTAX ERROR: missing \')\' in \"case\"-comparison");
  449.  
  450.  L=getlex();
  451.  if ((L->type!=1)||(L->number!=10)) ERROR (" SYNTAX ERROR: missing \"of\" in \"case\"-comparison");
  452.  L=getlex();
  453.  
  454.  LIST_OF_CASES();
  455.  cout<<L->type<<"^"<<L->number<<endl;
  456.  if ((L->type!=1)||(L->number!=11)) ERROR (" SYNTAX ERROR: missing \"end\" in \"case\"-comparison");
  457.  L=getlex();
  458.  if ((L->type!=2)||(L->number!=9)) ERROR (" SYNTAX ERROR: no \";\" at the end of \"case\"-comparison");
  459.  L=getlex();
  460. }
  461.  
  462.  
  463. void PROGRAM ()
  464. {
  465.  if ((L->type!=1)||(L->number!=0)) ERROR (" SYNTAX ERROR: not a program");
  466.  L=getlex();
  467.  if ((L->type!=2)||(L->number!=2)) ERROR (" SYNTAX ERROR: opening brace missing");
  468.  L=getlex();
  469.  while ( (L->type==1) &&
  470.        ( (L->number==1) || (L->number==2) ) )
  471.   {
  472.    L=getlex();
  473.    DESCRIPTION ();
  474.   }
  475.  while ((L->type!=2)||(L->number!=3))
  476.   {
  477.    OPERATOR();
  478.   }
  479.  if ((L->type!=2)||(L->number!=3)) ERROR (" SYNTAX ERROR: closing brace missing");
  480.   cout<<endl<<("  syntax is correct  ")<<endl;
  481. }
  482.  
  483.  
  484.  
  485. //------------------------------------------------------ MAIN ------------------------------------------------------
  486.  
  487. int main (int argc, char** argv)
  488. {
  489.  string s ;
  490.  //lexem* L;
  491.  open_file(argc,argv);
  492.  
  493.  
  494. // P->print();
  495. // while (q!=0)
  496. //  {
  497. //   P=getlex();
  498. //   cout<<endl;
  499. // P->print();
  500. //  }
  501.  
  502. L=getlex();
  503. PROGRAM ();
  504.  
  505.  mprog.close();
  506.  getchar();
  507.  return 0;
  508. }

Raw Paste


Login or Register to edit or fork this paste. It's free.