Day4 of #daily_code

Not so much time to code today. Despite that I want a few things to be shared.

-----------------------------------------------------

1. Firstly Let's understand the Token generation process in C/C++. Do you know what token is ? Generally token is the smallest possible unit of a program ?combining together which gives a meaningful program. To understand the token generation process let dive into some prerequisites.

We know that every program is first compiled to check if there is any syntactical error or semantic error in the program. If syntactical error is found?then compiler will produce error while compiling also called compile-time error. If all statements are syntactically perfect then compiler allows the source code to be converted into machine code. But if there is any semantic error like there is a non existing memory location but we are accessing it through pointer(concept of DANGLING pointer), will give error at run-time also called run-time error. Let's proceed a step further.

?Lexical analysis is the first phase in the compilation process. Lexical analyzer(scanner) scans the whole source program and when it finds meaningful sequence of character(lexemes) then it converts it into a token. Then 2 things come into picture..

??????????????????????1. Syntax Analyzer

??????????????????????2. Semantic Analyzer

Syntax analyzer looks the syntax of whole program if it is alright then Semantic analyzer transfers?each token into parse tree to store as a form of data structure. Now let's take an example.

?Suppose given int a=4; and a+++b;

Then token will be generated as (int : keyword), (a : variable), (= : operator), (4 : constant).

For 2nd statement token will be as (a : operand variable), (++ : increment operator), (+ : arithmetic operator), (b : operand variable).

?I hope the concept is clear. We will see an application of this token generation in upcoming point.

--------------------------------------------------------

2. We know what a switch statement means.

??--> It is a multi-conditional statement and takes one argument as input and matches with a case label to print its contents.

??--> The label of case should be an integral or character constant.

??--> Most important thing is that any statements out of any case label won't be executed and bypassed.

We will discuss what exactly the 3rd point means. First see the below program..

No alt text provided for this image
No alt text provided for this image

The program is throwing an error saying the statement will never get executed. It happens because when compiler compiles it then the syntax analyzer finds that the program is all good in syntax and then semantic analyzer comes. It scans again and also finds semantically perfect also. Why is it so because our declaration and all other statements are totally perfect. But out of case whatever is written will be bypassed by the compiler. So the program will give this warning as output.

In some compilers the program will give output as a garbage value. Why is so ??It is because as we know from lexical analysis all things are perfect. ?So all statements are executing but the statements out of switch case are being bypassed. So my variable p is created but it never be updated to 12 and hence giving us a garbage output with warning. See below program.

No alt text provided for this image

Here I have changed my program a little bit. I have declared the variable before switch to show the actual thing.

No alt text provided for this image

Hope it is clear now. This is it for today. Not have so much time to do plenty of things. But will catch up later with many more new concepts like these.

Thank You !


要查看或添加评论,请登录

Apurba Sundar Nayak的更多文章

  • Day3 of #daily_code

    Day3 of #daily_code

    Learnt a bunch of things today. ----------------------------------------------------- 1.

  • Day2 of #daily_code

    Day2 of #daily_code

    Learnt a bunch of things today…

  • Discussing different formats of scanf()

    Discussing different formats of scanf()

    scanf() is basically a function declared in stdio.h and it takes input from standard input stream i.

  • Day1 of #daily_code

    Day1 of #daily_code

    Learnt Shallow Copy and Deep Copy concept in C++ ------------------------------------------------ Generally if we want…

社区洞察

其他会员也浏览了