이거근데 아무리봐도 해준대로 듣고보니까 젤 앞에다가 하는게 맞는것같아서
따로 빼서 했는데 . . 아무리해도 parse error ^_ㅠ 근데 지금 이거만잡고몇시간있더니 넘힘드러
못찾겠다 왜 에러야 디버깅해도모르게썽 아나 왜이러지 얘 넘 속썩인다
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int token; /* holds the current input character for the parse */
/* declarations to allow arbitrary recursion */
int command(void);
int expr(void);
int term(void);
int factor(void);
int number(void);
int digit(void);
int prefix(void);
void error(void) //에러처리
{ printf("parse error\n");
exit(1);
}
void getToken(void) //토큰하나를 불러온다.
{ /* tokens are characters */
token = getchar();
}
void match(char c)
{if (token == c) getToken(); //char c가 토큰이랑 같으면 그것을 불러온다.
else error();
}
int command(void)
/* command -> expr '\n' */
{ int result =prefix();
if (token == '\n') /* end the parse and print the result */
printf("The result is: %d\n",result);
else error();
return result;
}
int prefix(void) //이부분 안된다 !연산자 최우선순위
{
int result;
if(token == '!') //젤앞에 !가 나오면 ㅜㅜㅜㅜㅜㅜ
{ result = !expr(); // 반대로..제발반대로되랏
}
else
{result = expr();}
return result;
}
int expr(void) //이 안에 비교연산도 다 들어가야 한다. result를 도출
/* 비교 연산을 쓸 때 true면 1을 리턴 , false면 0을 리턴시킨다*/
{
int result = term(); //우선순위에 따라 사칙연산 먼저 계산한다.
int value1,value2; //비교연산을 위해 저장 할 . .
while (token == '=' ) // == 구현부분
{
match('=');
match('=');
value1 = result;
value2 = expr();
if(value1 == value2) // 정말로 == 이면 1을 리턴
{
result = TRUE;
printf("TRUE\n");
}
else
{
result = FALSE; //아니면 0을 리턴
printf("FALSE\n");
}
}
while (token == '!' )
{
match('!');
if(token == '=') //!= 구현하는 것 ~가 아니다란 의미 2!=3 ->1
{
match('=');
value1 = result;
value2 = expr();
if(value1 != value2)
{
result = TRUE;
printf("TRUE\n");
}
else
{
result = FALSE;
printf("FALSE\n");
}
}
}
while (token == '>') // > 를 만나면 그 뒤에꺼 다 계산해서 끌어와야 함
{
match('>');
value1 = result;
value2 = expr();
if(value1 > value2) //정말로 > 이면 1을 리턴
{
result = TRUE;
printf("TRUE\n");
}
else
{
result = FALSE; //아니면 0 을 리턴
printf("FALSE\n");
}
}
while (token == '<') // > 를 만나면 그 뒤에꺼 다 계산해서 끌어와야 함
{
match('<');
value1 = result;
value2 = expr();
if(value1 < value2) // 정말로 < 이면 1을 리턴
{
result = TRUE;
printf("TRUE\n");
}
else
{
result = FALSE; //아니면 0을 리턴
printf("FALSE\n");
}
}
while (token == '+') // 더하기
{
match('+');
result += term();
}
while (token == '-') //빼기
{
match('-');
result -= term();
}
return result;
}
int term(void) //곱하기 나누기
/* term -> factor { '*' '/' factor } */
{
int result = factor(); //괄호와 넘버를 먼저 가져온 것을 result로 함
while (token == '*')
{
match('*');
result *= factor();
}
while (token == '/')
{
match('/');
result /= factor();
//0으로 나눌 때 에러 출력
}
return result;
}
int factor(void) //괄호를 총괄하는 함수, 넘버를 가져온다.
/* factor -> '(' expr ')' | number */
{ int result;
if (token == '(')
{ match('(');
result = expr();
match(')');
}
else
result = number();
return result;
}
int number(void) //ND
/* number -> digit { digit } */
{ int result = digit();
while (isdigit(token))
/* the value of a number with a new trailing digit
is its previous value shifted by a decimal place
plus the value of the new digit
*/
result = 10 * result + digit();
return result;
}
int digit(void) //D
/* digit -> '0' | '1' | '2' | '3' | '4'
| '5' | '6' | '7' | '8' | '9' */
{ int result;
if (isdigit(token))
{ /* the numeric value of a digit character
is the difference between its ascii value and the
ascii value of the character '0'
*/
result = token - '0';
match(token);
}
else
error();
return result;
}
void parse(void)
{ getToken(); /* get the first token */
command(); /* call the parsing procedure for the start symbol */
}
main()
{ parse();
return 0;
}
멍충아 제일처음 시작할때 토큰 하나 읽어야지
ㅇㅏ미안
아오 ㅋㅋㅋㅋㅋ멍청인가보닼ㅋㅋㅋㅋ아 자고싶다
command 에서 match(0); 하나 떄려주고 prefix() 호출해
글고 factor 에 \'(\' expr \')\' 순서로 되어있는데 안쪽 expr 을 prefix 로 바꿔야 된다. 지금 짠건 prefix 가 우선순위가 가장 낮으니까 다시 그놈부터 재귀 시작해야지 괄호 만나면
match(0); 후에 prefix호출하면 되던 비교연산자 다 안되네
암튼 땡큐 도저히 안되겠다 낼 겨스님한테 물어볼래 졸려 왕땡큐베리감사