如何使用C创建Pascal解析器 [英] how can create a pascal parser using C

查看:172
本文介绍了如何使用C创建Pascal解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有一个使用C进行扫描仪(LCG)pascal编程的扫描仪程序
如何使用我的扫描仪创建解析器?
请给我示例以使用我的扫描仪创建解析器

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

 #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;
}

推荐答案

我认为我们所有人都必须感谢您为编写某种可以以某种方式识别关键字的词法分析器而付出的努力,操作等
但是从词法分析器到语义解析,再到语言转换,都是一项巨大的工作,无法在快速解答"中解释,也不是那么容易负担.
这个项目还需要您要翻译的两种语言(以及它们拥有的所有方言)的丰富知识.
我想我能给您的唯一建议是,看看将C转换为Pascal或相反的现有试验.
您可以通过其他 [
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[^].


这篇关于如何使用C创建Pascal解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆