Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7879629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:56:28+00:00 2026-06-03T03:56:28+00:00

im using visual studio 2010 for C++ i implemented a code for a lexical

  • 0

im using visual studio 2010 for C++ i implemented a code for a lexical analyzer for C programming but i got an error says “missing type specifier – int assumed. Note: C++ does not support default-int” is there something wrong with my code? or i just have to change the compiler?
here is the code

    #include <stdio.h>
    #include <ctype.h>

    //Global variables
     int charClass;
    char lexeme [100];
    char nextChar;
    int lexLen;
    int token;
    int nextToken;
    FILE *in_fp, *fopen();

   // Function Declarations
   void addChar();
   void getChar();
   void getNonBlank();
   int lex();

   //Character classes
   #define LETTER 0
   #define DIGIT 1
   #define UNKNOWN 99

   //Token codes
   #define INT_LIT 10
   #define IDENT 11
   #define ASSIGN_OP 20
   #define ADD_OP 21
   #define SUB_OP 22
   #define MULT_OP 23
   #define DIV_OP 24
   #define LEFT_PAREN 25
   #define RIGHT_PAREN 26

   //Main
       main(){
        if ((in_fp = fopen("front.in", "r")) == NULL)
            printf("ERROR - cannot open front.in \n");
        else{
            getChar();
            do {
                lex();
            }while (nextToken != EOF);
          }
       }

       // lookUp

       int lookup(char ch){
       switch(ch){
       case '(':
           addChar();
           nextToken = LEFT_PAREN;
           break;

       case ')':
           addChar();
           nextToken = RIGHT_PAREN;
           break;

       case '+':
           addChar();
           nextToken = ADD_OP;
           break;

       case'-':
           addChar();
           nextToken = SUB_OP;
           break;

       case'*':
           addChar();
           nextToken = MULT_OP;
           break;

       case'/':
           addChar();
           nextToken = DIV_OP;
               break;

       default:
           addChar();
           nextToken = EOF;
           break;
       }
       return nextToken;
       }
       //AddChar

       void addChar(){
           if (lexLen <= 98) {
               lexeme[lexLen++] = nextChar;
               lexeme[lexLen] = 0;
           }
           else
               printf("Error - lexeme is too long \n");
       }

       //getChar
       void getChar(){
           if ((nextChar = getc(in_fp)) != EOF) {
               if (isalpha(nextChar))
                   charClass = LETTER;
               else if (isdigit(nextChar))
                   charClass = DIGIT;
               else charClass = UNKNOWN;
           }
           else
               charClass = EOF;
       }
       // getNonBlank
       void getNonBlank(){
           while (isspace(nextChar))
               getChar();
       }
       //lex
       int lex(){
           lexLen = 0;
           getNonBlank();
           switch (charClass){
           case LETTER:
                   addChar();
                   getChar();
                   while (charClass == LETTER || charClass ==                    DIGIT){
                       addChar();
                       getChar();
                   }
                   nextToken = IDENT;
                   break;
                   // parse ints lits
               case DIGIT:
                   addChar();
                   getChar();
                   while (charClass == DIGIT){
                       addChar();
                       getChar();
                   }
                   nextToken = INT_LIT;
                   break;

                //pares and ops
               case UNKNOWN:
                   lookup(nextChar);
                   getChar();
                   break;

                   //EOF
               case EOF:
                   nextToken = EOF;
                   lexeme[0] = 'E';
                   lexeme[1] = 'O';
                   lexeme[2] = 'F';
                   lexeme[3] = 0;
                   break;
           }
        // end of switch
           printf("Next toke is: %d, next lexeme is %s\n",nextToken, lexeme);
           return nextToken;
       }

so for this example ( sum + 47 ) / total
this is suppose to be the output

Next token is: 25 Next lexeme is (
Next token is: 11 Next lexeme is sum
Next token is: 21 Next lexeme is +
Next token is: 10 Next lexeme is 47
Next token is: 26 Next lexeme is )
Next token is: 24 Next lexeme is /
Next token is: 11 Next lexeme is total
Next token is: -1 Next lexeme is EOF

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:56:30+00:00Added an answer on June 3, 2026 at 3:56 am
       main(){
        if ((in_fp = fopen("front.in", "r")) == NULL)
            printf("ERROR - cannot open front.in \n");
        else{
            getChar();
            do {
                lex();
            }while (nextToken != EOF);
          }
       }
    

    should be changed like this :

     //  return type of main should be explicitly int.
       int  main(){
        if ((in_fp = fopen("front.in", "r")) == NULL)
            printf("ERROR - cannot open front.in \n");
        else{
            getChar();
            do {
                lex();
            }while (nextToken != EOF);
          }
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have compiled this using Visual Studio 2010 compiler and it has compiler error
I am using Visual Studio 2010 but has received an external library developed for
I'm using Visual Studio C# Express 2010 to make a console application. I've implemented
I am new to programming but attempting to learn. Im running visual studio 2010
I am using Visual Studio 2010 RC1. I define a resource Brush2 in app.xaml_:
I'm using Visual Studio 2010 RC. Would it be possible that after doing my
I am using Visual Studio 2010 Ultimate Beta 2. What is the relevant namespace/classes
I'm using Visual Studio 2010 RTM with .NET/Entity Framework 4 RTM with a model
i am using visual studio 2010. i have a three dimensional structure array which,
I am using Visual Studio 2010. I've donwloaded an updated class (i.e. UpdatedClass.cs) which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.