1 1 vote What is the diffrence between Lexical error , Syntax error and semantic error..? Please explain with this example void main() { int total-value, Num=2,sum=5,var1; 5=Num; var1=2; sum=num*1; if(sum=var1) { sum=sum+1 } } Compiler Design compiler-design lexical-analysis descriptive + – Sahil1994 1.3k views answer comment Share Follow Print See all 9 Comments 9 9 Comments reply joshi_nitish commented Dec 12, 2017 reply Follow flag void main() { int ${\color{Red}total-value}$, Num=2,sum=5,var1; 5=Num; var1=2; sum=${\color{Green} num}$*1; if(sum=var1) { sum=sum+1 } } ${\color{Red} red}$ -> lexical errors ${\color{Blue} blue}$-> syntax errors ${\color{Green} green}$-> semantic errors 1 1 replyShare Anu007 commented Dec 12, 2017 reply Follow flag lexical errors : which comes while design DFA. syntax errors : while creating while drawing parse tree from grammer. semantic errors: which created while semantic is wrong parse tree. syntax errors : here 5= num is not according to grammar i,e. variable stored constant not reverse + sum=sum+1 since it does not end with a semicolon. semantic errors : sum=num*1; since in declaration Num is used i.e. num is not declared (is not not syntax error since syntax tree is possible + DFA also for token) 0 0 replyShare Anu007 commented Dec 12, 2017 reply Follow flag nitish why lexical error. 0 0 replyShare joshi_nitish commented Dec 12, 2017 reply Follow flag @Anu sir, total-value is also there which is lexical error since identifiers are char(char + digit)* 1 1 replyShare Ashwani Kumar 2 commented Dec 12, 2017 reply Follow flag total-value : No token will be generated as there is no such pattern or RE or DFA which can recognize this lexeme, hence lexical error. sum= sum +1 : Every statement should be terminated by semicolon (;) which is not present here, syntax violated, hence syntax error. 5=num : Syntax voilated, hence Syntax error sum = num*1; : No lexical error, no syntax error, but num is used which is not yet defined, hence semantic error 0 0 replyShare Anu007 commented Dec 12, 2017 reply Follow flag yes nitish :) 0 0 replyShare Sahil1994 commented Dec 12, 2017 reply Follow flag @Anu total−value since we cannot use hyphen in variable declaration i think so thatz y iot will be lexical error...we need to use underscore _ 0 0 replyShare Sahil1994 commented Dec 12, 2017 reply Follow flag @Anu007 @Nitish but the solution which i have saying 5=Num; // semantic error not syntax error..please confirm on this....? void main() { int total-value, Num=2,sum=5,var1; // total-value Lexical error as hyphen is not allowed 5=Num; // semantic error var1=2; sum=num*1; // num is undeclared variable as C is case sensitive, so Num and num is different (semantic error) if(sum=var1) // no error { sum=sum+1 // semicolon missing syntax error } 0 0 replyShare joshi_nitish commented Dec 12, 2017 reply Follow flag 5=var; is syntax error, which is known as L-value error. 0 0 replyShare Please log in or register to add a comment.
0 0 votes 'total-value' is a lexical error. Lexical analyzer won't be able to identify the token. '5=Num;' and 'sum=sum+1' are syntax errors. These tokens don't qualify as valid syntax. 'sum=num*1;' is a semantic error as only semantic analyzer will be able to check that 'num' is an undeclared variable. gmrishikumar answered Jan 7, 2019 gmrishikumar comment Share Follow 0 reply Please log in or register to add a comment.