2017-03-03 6 views
4

나는 토큰을 궁금해되는 방법 그들은 컴파일러에 의해 평가,하지만 구문 성명이 유효하기위한 필수 토큰으로 공간을 고려하지 않습니다공간 바이너리 또는 단항 연산자의 평가에 미치는 영향

예를 들어.

#include<stdio.h> 
int main(){ 

     int first=1,second=3; 
     int res=first+++++second; 
     printf("%d \n",res); 
     return 0; 
} 

다음과 같은 오류 제공합니다 :

rough3.c:7:17: error: lvalue required as increment operand int res=first+++++second;

을하지만 단순히 두 개의 후위 (++)와 접두사 사이에 ""추가하여 (++) 는 잘 작동하는 것 같다.

#include<stdio.h> 
int main(){ 

     int first=1,second=3; 
     int res=first++ + ++second; 
     printf("%d \n",res); 
     return 0; 
} 

잘 인쇄 값 5 은 내가 알고 싶은이 question보다는 정의되지 않은 동작으로 보았다 작동 :
컴파일러는 표현 사이에 공백이 중복 여부 것을 결정 하는가?
이 표현식을 평가하기 위해 우선 순위와 연관성을 함께 사용하면 어떻게됩니까?

+3

[ "Maxunch]"(https://en.wikipedia.org/wiki/Maximal_munch) 규칙을 참조하십시오. – molbdnilo

+1

이것은 복제본입니다. 이 질문은 이번 주 초에 요청되었으며 다른 질문의 사본으로 마감되었습니다. –

+0

@molbdnilo 감사합니다. – OldMontMonk

답변

4

(6.4 어휘 요소)이 선언

int res=first+++++second; 

위한 따라서

4 If the input stream has been parsed into preprocessing tokens up to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token. There is one exception to this rule: header name preprocessing tokens are recognized only within #include preprocessing directives and in implementation-defined locations within #pragma directives. In such contexts, a sequence of characters that could be either a header name or a string literal is recognized as the former.

입력 스트림이 사전에 해석되어 있으면 에 이름 first 후 최대 토큰 first 식별자 다음에 다음으로 긴 전처리 토큰은 ++

입니다. 144,

는 그런 다음 긴 전처리 토큰은 다시 등 ++

int res=first++ ++ +second; 
      ^^ ^^ 

하고있다.

그래서이 토큰은이 선언은

int res= (first++)++ + second; 

처럼 간주되어 표현 (first++)lvalue하지 않기 때문에 컴파일러가 오류를 발행하는 C 문법에 따르면

int res=first++ ++ + second ; 
      ^^ ^^^^^^^^^^

생산됩니다. 따라서 접미사 연산자 ++은 표현식에 적용되지 않을 수 있습니다.

+0

도움이되는 설명에 감사드립니다. – OldMontMonk

+0

공백 자체가 토큰이 아니기 때문에 두 번째 사례가 성공적으로 컴파일되므로 토큰 중 구분 기호 역할을하므로 평가는 다음 순서로 발생합니다. 'int res = first ++ + ++ second;' 오류없이 컴파일합니다. :) – OldMontMonk

2
int res=first+++++second; 

int res = (first++)++ +second; 

로 해석 따라서 (first++)은 L 값이 아니기 때문에 컴파일 오류.
int res=first++ + ++second; 

하지만

는 올

int res = (first++) + (++second); 

로 해석됩니다. 표준 C에 따르면

+0

'+++++++++'질문이 마음에 들지 않으십니까? –

+0

답변 주셔서 감사합니다 :). – OldMontMonk