Each language specifies its syntax in a context free grammar, you can find pythons CFG here as an example:https://docs.python.org/3/reference/grammar.html
This is basically a series of rules, where you can start with a symbol, and use the production rules to develop a representation of your source code as a tree structure.
A tool like yacc will analyze your source code using these production rules to create a representation of your source code in the form of an abstract syntax tree https://en.m.wikipedia.org/wiki/Yacc?wprov=sfla1
At this point, if there are syntax errors, yacc will throw an error because it can't follow the rules in the grammar to build a tree. When the tree is built, you can perform analysis like type checking, and making sure variables are declared before being used.
The AST is further analyzed, and split into basic blocks. A control flow graph is created, which allows the program to optimize the code in many ways, in addition to things like allocating registers on the processor. https://en.wikipedia.org/wiki/Control_flow_graph?wprov=sfla1
The control flow graph can then be converted to machine language using a translation scheme, which is different for each processor instruction set and architecture
Compiler design is a complicated endeavor that uses lots of CS theory, and is a big area of academic research