[질문] 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 환경입니다.

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<stdio.h>
%}
%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 am are
noun pig I

이런식으로 하는데

verb is am
정도만 해도
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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.