title: How do i implement a semantic checker for given language
body: <pre><code>for(var x in z) {
   a = x + 1 + 2;
   foo();
   }
   function bar() {}
   for(t in []){
   function hello(a) {
   a = t + 'hello' + 'world';
   }
   bar();
   hello();
   }
   hello();
   function hello() {}
   15 * 30;
   1 + 2 + 3;
   a = 3 - 2 - 1;
   a + 10;
  </code></pre>
  
  <p>Lets say that we have a semantic rule which is when a function is called, the function shall be declared before in current scope or in the parent scope.</p>
  
  <p>So since foo(); is not declared in above program(line 3 ) we need to print an error message.</p>
  
  <p>For line 13 hello() is no declared so we need to print an error message.</p>
  
  <p>We have bison/flex implementation however attribute grammer implementation is missing.</p>
  
  <p>Parser:</p>
  
  <pre><code>%{
  #include &lt;stdio.h&gt; 
  void yyerror(const char * msg){
          printf("%s\n",msg);
  }
  %} 
  %token tFOR tIN tSEMICOLON tLPAR tLBRKT tLT tSTAR tPLUS tPERCENT tINT tREAL tSTRING tWHILE tVAR tCOMMA tRPAR tRBRKT tRBRACE tGT tEQ tMINUS tNOT tIDENT tIF tCOLON tLBRACE tELSE tEQCHECK tFUNCTION
  %left tMINUS tPLUS
  %left tSTAR
  %left tPERCENT
  %left tLT tGT tEQCHECK
  %left tNOT
  
  %%
  prog: statementList
  ;
  
  statementList: statementList statement
               | statementList tSEMICOLON
               |
  ;
  
  statement: assign
           | if
           | expr
           | while
           | for
           | functionCall
           | functionDeclaration
  ;
  
  assign: tIDENT tEQ expr
        | tVAR tIDENT tEQ expr
  ;
  
  if: ifPart elsePart
  ;
  
  ifPart: tIF tLPAR expr tRPAR statementBlock
  ;
  
  elsePart: tELSE statementBlock 
          | 
  ;
  
  while: tWHILE tLPAR expr tRPAR statementBlock
  ;
  
  for: tFOR tLPAR tIDENT tIN expr tRPAR statementBlock
     | tFOR tLPAR tVAR tIDENT tIN expr tRPAR statementBlock
  ;
  
  functionDeclaration: tFUNCTION tIDENT tLPAR exprList tRPAR statementBlock
                     | tFUNCTION tIDENT tLPAR tRPAR statementBlock
  ;
  
  statementBlock: tLBRACE statementList tRBRACE
  ;
  
  functionCall: tIDENT tLPAR exprList tRPAR
              | tIDENT tLPAR tRPAR
  ;
  
  expr: tIDENT
      | tREAL
      | tINT
      | tSTRING
      | tLBRKT tRBRKT
      | tLBRKT exprList tRBRKT
      | tLBRACE tRBRACE 
      | tLBRACE propertyList tRBRACE
      | tNOT expr
      | expr tPLUS expr
      | expr tMINUS expr
      | expr tSTAR expr
      | expr tEQCHECK expr
      | expr tLT expr
      | expr tGT expr
  ;
  
  exprList: expr
          | exprList tCOMMA expr
  ;
  
  propertyList: tIDENT tCOLON expr
              | propertyList tCOMMA tIDENT tCOLON expr
  ;
  
  %%
  int main() {
    return yyparse();
  }
  </code></pre>
  
  <p>Scanner:</p>
  
  <pre><code>%{
  #include "parser.tab.h"
  %}
  %%
  in      return tIN;
  function      return tFUNCTION;
  for      return tFOR;
  while    return tWHILE;
  var      return tVAR;
  ";"      return tSEMICOLON;
  "("      return tLPAR;
  ")"      return tRPAR;
  "["      return tLBRKT;
  "]"      return tRBRKT;
  "&lt;"      return tLT;
  "&gt;"      return tGT;
  "*"      return tSTAR;
  "+"      return tPLUS;
  "%"      return tPERCENT;
  ","      return tCOMMA;
  "{"      return tRBRACE;
  "}"      return tLBRACE;
  "=="      return tEQCHECK;
  "="      return tEQ;
  "-"      return tMINUS;
  "!"      return tNOT;
  "-"?[0-9]+  return tINT;
  ("-"[0-9]+"."[0-9]+)|([0-9]*"."[0-9]+)   return tREAL;
  ('[^']*')|(["][^"]*["]) return tSTRING;
  [a-zA-Z_][a-zA-Z_0-9]*  return tIDENT;
  [ \t\n] 
  .      return yytext[0];
  %%
  </code></pre>