[질문] flex와 bison 관련 질문 다시 올립니다.

ddangkyoung의 이미지

도저히 원인을 모르겠습니다...

flex와 bison으로 기본적인 영어를 인식하는 lexer와 parser를 만들어봤습니다.
token외의 것을 입력하면 don`t recognized라는 문장이 잘 뜨는데
token 변수 이런식으로 add_word를 하려고 하면
syntax error 라는 말이 계속 뜨네요;

예를들어
verb is am are
라고 입력하면 verb 토큰에 해당하는 is am are가 리스트에 추가되고
다시
is 라고 입력하면 is를 verb 토큰으로 인식해야 되는데
여기서 syntax에러가 나버립니다.;

참고로 컴파일은
flex example.l
bison -d example.y
gcc -o example lex.yy.c example.tab.c -ly -lfl
이렇게 했고요

cygwin 환경입니다.
커맨드 입력은 다음과 같이 했는데요

verb is am are

이러면 원래 is am are를 verb로 인식해야되는데
저상태에서
입력을 더이상 받아들이지를 않습니다.

verb is
라고만 입력하면

is를 verb로 인식해야 하는데

is 라고 치면

syntax error이라는 문장이 뜨고요.

원인을 모르겠네요 정말;
책에 있는 그대로 썼을 뿐인데...

example.l 코드
 
 
%{
#include "example.tab.h"
#define LOOKUP 0
int state;
%}
 
%%
\n	{ state=LOOKUP;}
\.\n	{ 
		state=LOOKUP; 
		return 0;
	}
^verb	{ state=VERB; }
^adj	{ state=ADJECTIVE; } 
^adv	{ state=ADVERB; }
^noun	{ state=NOUN; }
^prep	{ state=PREPOSITION; }
^pron	{ state=PRONOUN; }
^conj	{ state=CONJUNCTION; } 
[a-zA-Z]+ {
	if(state !=LOOKUP) {
		 add_word(state, yytext);
 
	} else {
		switch(lookup_word(yytext)) {
		case VERB: 		
			return(VERB);
		case ADJECTIVE: 	
			return(ADJECTIVE);
		case ADVERB:		
			return(ADVERB);
		case NOUN:		
			return(NOUN);
		case PREPOSITION: 	
			return(PREPOSITION);
		case PRONOUN:		
			return(PRONOUN);
		case CONJUNCTION: 	
			return(CONJUNCTION);
		default:
			printf("%s: don't recognize\n", yytext);
		}
	}
}
 
\. ;
%%
 
struct word {
	char *word_name;
	int word_type;
	struct word *next;
};
 
struct word *word_list;
extern void *malloc();
 
int 
add_word(int type, char *word)
{
	struct word *wp;
	if(lookup_word(word) != LOOKUP) {
		printf("!!! warning: word %s already defined \n", word);
		return 0;
	}
 
	wp = (struct word*) malloc(sizeof(struct word));
	wp->next = word_list;
 
	wp->word_name = (char*) malloc(strlen(word)+1);
	strcpy(wp->word_name, word);
	wp->word_type = type;
	word_list = wp;
	return 1;
}
 
int 
lookup_word(char *word)
{
	struct word *wp=word_list;
 
	for(; wp; wp->next) {
		if(strcmp(wp->word_name, word) ==0)
			return wp->word_type;
	}
	return LOOKUP;
}

example.y 코드

%{
#include
%}
%token NOUN PRONOUN VERB ADVERB ADJECTIVE PREPOSITION CONJUNCTION
%%
sentence: subject VERB object { printf("Sentence is valid.\n"); }
  ;}
subject:  NOUN
  | PRONOUN
  ;
object:  NOUN
  ;
%%
 
 
extern FILE *yyin;
main()
{
	do
	{
		yyparse();
	}while(!feof(yyin));
}
yyerror(s)
char *s;
{
	fprintf(stderr, "%s\n",s);
}
terzeron의 이미지

일단 ddangkyoung님께서 입력하신 내용이 두 줄이네요.
verb is
is

그런데 첫번째 문장은 동사라고 is를 등록하는 것이고, 두번째는 실제 문장이 문법에 맞는지 확인하기 위해 테스트로 입력해보는 거라고 추측해봅니다.

하지만 is라는 문장은 영어문법으로도 여기 파서의 문법으로도 옳지 않습니다. 그래서 syntax error가 발생한 것이죠.

sentence: subject VERB object
subject: NOUN | PRONOUN
object: NOUN

이 3가지 문법 규칙을 보면 문장은 주어 동사 목적어로 이루어지고
주어는 명사와 대명사, 목적어는 명사라고 나와 있습니다.
일단 명사와 동사를 모두 등록하고 문법에 맞는 문장을 입력해야 제대로 실행되겠죠.

verb am are is
pronoun i you he she they it this that
noun boy girl desk book

i am boy
you are girl

이렇게 입력해야 syntax error가 발생하지 않을 겁니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.