Thread: lexical analyzer code error

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    lexical analyzer code error

    i am running this code but i am geting few errors .as shown in the imagelexical analyzer code error-17-03-2013-6-51-43-pm-jpg
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <conio.h>
    #include <string>
    
    
    
    
    
    
    using namespace System;
    
    
    #define T_SEMICOLON ';'
    #define T_LPAREN '('
    #define T_RPAREN ')'
    #define T_ASSIGN '='
    #define T_DIVIDE '/'
    
    
    #define T_WHILE 257
    #define T_IF 258
    #define T_RETURN 259
    
    
    #define T_IDENTIFIER 268
    #define T_INTEGER 269
    #define T_DOUBLE 270
    #define T_STRING 271
    
    
    #define T_END 349
    #define T_UNKNOWN 350
    
    
    
    
    struct token_t {
    	int type;
    	union{
    		char stringValue [256];
    		int intValue;
    		double doubleValue;
    	} val;
    };
    //array<System::String ^> ^args
    int main(int argc,char *argv[])
    {
        
    	
    	struct token_t token; 
    	InitScanner();
    	while (ScanOneToken(stdin, &token) !=T_END);
    	
    		
    		
    	
    	return 0;
    	
    }
    static void InitScanner()
    {
    create_reserved_table();
    insert_reserved("WHILE",T_WHILE)
    insert_reserved("IF",T_IF)
    insert_reserved("RETURN",T_RETURN)
    }
    static int ScanOneToken(FILE *fp,struct token_t *token)
    {
    	int i,ch,nextch;
    	ch =getc(fp);
    	while (isspace(ch))
    		ch = getc(fp);
    	switch(ch)
    	{
    	case'/':
    		nextch=getc(fp);
    		if(nextch=='/'|| nextch =='*');
    		else
    			ungetc(nextch,fp););
    	case';':case',':case'=':
    		token->type =ch;
    		return ch;
    
    
    	case 'A':case'B':case'C':case'D':case'E':case'F':case'G':case'H':case 'I':case'J':case'K':case'L':case'M':case'N':case'O':case'P':
    	case 'Q':case'R':case'S':case'T':case'U':case'V':case'W':case'X':case'Y':case'Z':
    		token->val.stringValue[0]=ch;
    		for (i=1;isupper(ch =getc(fp));i++)
    			token->type =lookup_reserved(token->val.stringValue);
    		return token->type;
        case 'a':case'b':case'c':case'd':case'e':case'f':case'g':case'h':case 'i':case'j':case'k':case'l':case'm':case'n':case'o':case'p':
    	case 'q':case'r':case's':case't':case'u':case'v':case'w':case'x':case'y':case'z':
    			token->type = T_IDENTIFIER;
    			token->val.stringValue[0] = ch;
    			for (i=1; islower(ch= getc(fp));i++)
    				token->val.stringValue[i]=ch;
    			ungetc(ch,fp);
    			token->val.stringValue[i] ='\0';
    			if (lookup_symtab(token->val.stringValue)==NULL)
    				add_symtab(token->val.stringValue);
    			return T_IDENTIFIER;
    			case '0':case'1':case'2':case'3':case'4':case'5':case'6':case'7':case '8':case'9':
    				token->type=T_INTEGER;
    				token->val.intValue = ch -'0';
    				while (isdigit(ch = getc(fp)))
    					token->val.intValue = token->val.intValue*10+ch-'0';
    				ungetc(ch,fp);
    				return  T_INTEGER;
    
    
    			case EOF:
    				return T_END;
    				
    			default:
    				token->val.intValue = ch;
    				token->type = T_UNKNOWN;
    				return T_UNKNOWN;
    	}
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You need to declare the functions InitScanner() and ScanOneToken() before using them. Best place is before main()
    Kurt

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Quote Originally Posted by ZuK View Post
    You need to declare the functions InitScanner() and ScanOneToken() before using them. Best place is before main()
    Kurt
    i am still getting errors doing that toolexical analyzer code error-17-03-2013-8-45-12-pm-jpg

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    you have to define and declare all the other missing functions as well.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    actually i am doing it from this article http://dragonbook.stanford.edu/lectu...l-Analysis.pdf i am new to c++ can you help me out with this

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by achme View Post
    actually i am doing it from this article http://dragonbook.stanford.edu/lectu...l-Analysis.pdf i am new to c++ can you help me out with this
    I can help if you have a specific question.
    Kurt

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    ok like if i want to make create_reserved_table(); so how am i going to do that

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    As this is c++ you don't have to do much to create a table of reserved words.
    a std::map<std::string,int> will do.

    insert_reserved would just add the reserved word to that map, and lookup_reserved would just look up the token type given a string.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ parser/ simplified lexical analyzer
    By XodoX in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2010, 09:21 AM
  2. Lexical Analyzer
    By ammad in forum C++ Programming
    Replies: 8
    Last Post: 11-18-2009, 05:59 PM
  3. simple lexical analyzer
    By ^Son_Gokou08 in forum C Programming
    Replies: 6
    Last Post: 08-25-2009, 07:55 AM
  4. Scanner? Lexical analyzer? Tokenizer?
    By audinue in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-23-2008, 10:32 PM
  5. Lexical analyzer for C
    By nishkarsh in forum C Programming
    Replies: 4
    Last Post: 08-26-2008, 08:05 AM