4 Replies - 397 Views - Last Post: 19 September 2019 - 05:40 PM Rate Topic: -----

#1 pain9901   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-September 19

can anyone help me out to write a basic program for checking a syntax?

Posted 14 September 2019 - 06:17 AM

this is my program but still syntax error when i run it.., please help me out




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

int token;
void parse();
void command();
void getToken();
void error();
int expr();
int var();
int string();
void match(char);




void match(char c)
{
	if (token==c)
	{
		getToken();
	}
	else
	{
		error();
	}
}

int string()
{
	int result;
	while (token != ';')
	{
	if (isalpha(token))
	{
		result = token;
		match(token);
	}
	else 
	{
		error();
	}

	result = result & result;

	}
}

int var()
{
	int result;
	if (isalpha(token))
	{
		result = token;
		match(token);
	}
	else 
	{
		error();
	}
	return result;
}


int expr()
{
	int result = var();
	 while (token== 's')
	 {

		match('s');
		return result;
	 }
	 while(token == 't')
	 {
	 	match('t');
	 	return result;

	 }
	 while(token == 'r')
	 {
	 	match('r');
	 	return result;
		
	 }
	 while (token == 'i')
	 {
	 	match('i');
	 	return result;
	 }
	 while (token == 'n')
	 {
	 	
		match('n');
		return result;
	 }
	 while (token == 'g')
	 {
	 	
		match('g');
		return result;
	 }
	 while (token == ' ')
	 {
	 	
		match(' ');
		return result;
	 }
	 
	 
	
		result = var();
	 return result;
	 while (token == '=')
	 {
	 	
		match('=');
		return result;
	 }

	 	
		result = string();
		
	return result; 
	 while (token == ';')
	 {
	 	match(';');
		return result;
	 }
}

void error()
{

	printf("Syntax error");	
}

void getToken()
{

	token=getchar();

}




void command()
{
	int result = expr();

	if (token == '\n')
	{

		printf("the string is  %s\n",result);

	} 
	else
	{
		error();
	}

}

void parse()
{

	getToken();
	command();
}


int main ()
{

	parse();

	return 0;
:code:

This post has been edited by modi123_1: 14 September 2019 - 07:45 AM
Reason for edit:: In the future please use the [code] tag button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: can anyone help me out to write a basic program for checking a syntax?

#2 jimblumberg   User is offline

  • member icon

Reputation: 5761
  • View blog
  • Posts: 17,640
  • Joined: 25-December 09

Re: can anyone help me out to write a basic program for checking a syntax?

Posted 14 September 2019 - 07:32 AM

Quote

this is my program but still syntax error when i run it.

What exactly is this "syntax error"?

If you are getting compile/linker warnings/errors please post the complete error messages, all of them exactly as they appear in your development environment.


In future please use code tags when posting code.

:code:


Jim
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,993
  • Joined: 05-May 12

Re: can anyone help me out to write a basic program for checking a syntax?

Posted 14 September 2019 - 10:06 AM

Just scanning the code, it looks like it is the output of the program from line 136 due to using an uninitialized variable on at line 53.
Was This Post Helpful? 0
  • +
  • -

#4 pain9901   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-September 19

Re: can anyone help me out to write a basic program for checking a syntax?

Posted 19 September 2019 - 04:42 PM

/*SYNTAX RULE
comand -> expr '\n'
epr -> term | '+' term
term -> factor | '*' factor
factor -> '('expr')' | number
number -> digit
digit -> 0|1|2|3|4|5|6|7|8|9 */

#include<ctype.h>
#include<stdlib.h>
#include<stdio.h>
char token;

void parse();
void command();
void getToken();
void error();
char expr();
char var();
char string();
void match(char);




void match(char c)
{
	if (token==c)
	{
		getToken();
	}
	else
	{
		printf("match");
		error();
	}
}

char string()
{
	char result;

	if (!isdigit(token))
	{
		result = token - '0';
		match(token);
		
	}
	else 
	{
		printf("string");
		error();
	}
	return result;

	
} //string shit

char var()
{
	char result;

	if (!isdigit(token))
	{
		result = token - '0';
		match(token);
		
	}
	else 
	{
		printf("var");
		error();
		
	}
	return result;
}


char expr()
{
	char result = var();
	
		if (token=='g')
		{
		
			
			match('s');
	 		match('t');
	 		match('r');
	 		match('i');
			match('n');
			match('g');
			match(' ');
			result = var();
			match('='); 
			result = string();
			 match(';');

	
		}
		else
		{
			printf("expr");
			error();
		}
	return result;
	 
}

void error()
{

	printf("Syntax error");	
}


void getToken()
{

	token=getchar();

}




void command()
{
	char result = expr();

	if (token=='\n')
	{

		printf("You are correct!!");

	} 
	else
	{
		printf("command");
		error();
	}

}

void parse()
{

	getToken();
	command();
}


int main ()
{

	parse();

	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,993
  • Joined: 05-May 12

Re: can anyone help me out to write a basic program for checking a syntax?

Posted 19 September 2019 - 05:40 PM

You posted updated code. Do you have a new question?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1