Click here to Skip to main content
15,881,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey, i have a scanner program use C to scanner(LCG) pascal programing
how to create parser use the my scanner?
please give me example to create parser use my scanner

C++
 #include<stdio.h>
 #include<ctype.h>
 #include<string.h>
 #include <cstdio>

void keyw(char *p);
int i = 0, id = 0, kw = 0, num = 0, op = 0;
char keys[32][10] = { "auto", "break", "case", "char", "const", "continue", "default",
					  "do", "double", "else", "enum", "extern", "float", "for", "goto",
					  "if", "int", "long", "register", "return", "short", "signed",
					  "sizeof", "static", "struct", "switch", "typedef", "union",
					  "unsigned", "void", "volatile", "while"
					};

int main(void)
{
	char ch, str[25], seps[16] = " \t\n,();{}[]#\"<>", oper[] = "!%^&*-+=~|.<>/?";

	int j;
	char fname[50];
	FILE *f1;

	printf("Masukkan file yang ingin di scan contohnya(drive:\\nama_folder\\nama_file)\n");
	printf("Masukkan juga ekstensi filenya .c .pas .txt \n\n");
	scanf("%s", fname);
	f1 = fopen(fname, "r");

	if (f1 == NULL)
	{
		printf("File Tidak Ada Atau File Tidak Ditemukan");
		getchar;
		return 0;
	}
	while ((ch = fgetc(f1)) != EOF)
	{
		for (j = 0; j <= 14; j++)
		{
			if (ch == oper[j])
			{
				printf("%c operator\n\n", ch);
				op++;
				str[i] = '\0';
				keyw(str);
			}
		}
		for (j = 0; j <= 14; j++)
		{
			if (i == -1)
				break;
			if (ch == seps[j])
			{
				if (ch == '#')
				{
					while (ch != '>')
					{
						printf("%c", ch);
						ch = fgetc(f1);
					}
					printf("%c file header\n\n", ch);
					i = -1;
					break;
				}
				if (ch == '"')
				{
					do
					{
						ch = fgetc(f1);
						printf("%c", ch);
					} while (ch != '"');
					printf("\b argument\n\n");
					i = -1;
					break;
				}

				str[i] = '\0';
				keyw(str);
			}
		}
		if (i != -1)
		{
			str[i] = ch;
			i++;
		}
		else
			i = 0;
	}
	printf("Keywords: %d\nIdentifiers: %d\nOperators: %d\nAngka: %d\n", kw, id, op, num);
}

void keyw(char *p)
{
	int k, flag = 0;
	for (k = 0; k <= 31; k++)
	{
		if (strcmp(keys[k], p) == 0)
		{
			printf("%s keyword\n\n", p);
			kw++;
			flag = 1;
			break;
		}
	}
	if (flag == 0)
	{
		if (isdigit(p[0]))
		{
			printf("%s bilangan(angka) \n\n", p);
			num++;
		}

		else
		{
			if (p[0] != 13 && p[0] != 10)
				if (p[0] != '\0')
				{
					printf("%s identifier \n\n", p);
					id++;
				}
		}
	}
	i = -1;
}
Posted
Updated 24-May-15 2:43am
v3
Comments
OriginalGriff 23-May-15 5:40am    
Frankly, that's unreadable - there is no indentation, even if I try to edit it and code block it (nor was there in the previous version of this question), there are no comments, and the code is not even slightly self documenting. You could at least have used variable names which reflect what they are there for instead of names you find easy to type!

As a result, I'm not looking at your code - why would I want to waste my time if you can't be bothered with it?
And without it, your question makes no sense whatsoever.

So use the "Improve question" widget to indent your code (and use the "code" widget to surround it with pre tags to preserve the formatting) after you have made the code readable to someone who didn't write it! And then explain what part of your task is giving you difficulties.

1 solution

I think that we all here have to appreciate the effort you made to write a sort of lexer that could, in some way, recognize keyword, ops, etc.
But from a lexer to the semantic parsing, and up to language conversion is a huge work that cannot be explained in a 'quick answer', nor is so easy to afford.
This project requires also a strong knowledge of both languages you want to translate (and of all dialects that they have).
The only suggestion I think I can give you is to have a look to existing trials made to convert C to Pascal and the reverse.
You can find them by simply googling one way[^] or the other[^].
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900