Terrorists believed to be Basque separatists shot to death two Army officers and a private when they opened fire on a military vehicle in Madrid today.

The attack came just five days before general elections in which Socialist Prime Minister Felipe Gonzalez is seeking a second term and apparently was aimed at destabilizing the democratic process in Spain.

Among the dead was Maj. Ricardo Saenz de Ynestrillas, a well-known rightist officer who in 1979 served a one-year jail sentence for plotting a coup to overthrow then-prime minister Adolfo Suarez.

Witnesses said two men and a woman sprayed an Army staff car with machine-gun fire at lunch time as it stopped at a traffic light on a busy street near the city's outskirts. Killed in the attack along with Saenz de Ynestrillas were Lt. Col. Carlos Besteiro and the vehicle's driver, Francisco Casillas.

Police sources said the ambush was likely the work of the Basque separatist organization ETA Basque Homeland and Liberty , which has been responsible for more than 500 deaths since it began a terrorist campaign in 1968 aimed at the secession of the Basque provinces in the north from the rest of Spain.

Gonzalez's government has claimed that it has succeeded in reducing Basque political violence, but the terrorist issue remains at the forefront of campaign themes for the legislative election Sunday. The conservative opposition to the Socialist government has promised increased measures against terrorism should it be elected.

The Socialists are cautiously optimistic that they will gain an outright majority for a second term on Sunday. Although ETA's activities are designed to prompt a right-wing backlash, their effect could be to cause Spaniards to close ranks behind the present government.

Gonzalez has shown no signs of concern about the election. Opinion polls throughout his term have given him a consistently high rating, and present indicators show that he will win by an overall majority, albeit a reduced one. In 1982 the Socialist Party won 202 seats in the 350-member Congress of Deputies, the lower house, and the conservative opposition, which runs on a ticket called Popular Coalition, placed second, with 106 seats.

Gonzalez's political opponents have failed to create viable alternatives to challenge him. On the left of the Socialist Party are two small communist parties that are locked in bitter rivalry to win essentially the same voters. There are also two minority centrist parties, one of them headed by former prime minister Suarez, that have further split the non-Socialist vote.

The conservative Popular Coalition is headed by former Francoist minister Manuel Fraga Iribarne, 63, and has made little headway, according to the opinion polls.

According to a western ambassador in Madrid, Gonzalez has established "very firm control over the political center." Victor Perez Diaz, a University of Madrid sociologist, said in an interview that Gonzalez owes his success to his powers of communication and to his middle-of-the-road message: "Gonzalez has put across an ambiguous message of stability, reform and change that satisfies most people."

Gonzalez broke traditional socialist rules as soon as he gained power by implementing orthodox economic policies to control public spending, drawing praise from conservative business circles.

"Gonzalez has been courageous and correct on the economic front," Pedro Schwartz, a professional economist and member of the conservative opposition, said in an interview.

The criticism has come from the left. "Gonzalez has sliced the cake in such a way that those who had more have got more and those who were poor are poorer," said Julian Ariza, a communist labor union leader. Ariza contended that real income had dropped by an average 5 percent during the last four years and that the earnings of Spaniards in the public sector had been cut by 11 percent.

However, Gonzalez is adamant that in order to redistribute income and sponsor welfare, wealth has to be created first. His austerity policy has turned around Spain's economic outlook: A deficit of $4 million when he came to power has been turned into a $2.7 billion surplus at the end of last year, and inflation is down from 14 percent to under 8 percent.

The negative side of his policies is that unemployment has grown by about 700,000 in the past four years.

The Gonzalez government has contained the number of ETA attacks since it came to office in 1982. From 1979 to 1981, more than 100 persons were killed in Basque violence; in the past two years, the number has been fewer than 50. Part of the Socialist success is attributed to closer cooperation with the police in France, where ETA terrorists used to seek refuge.

Police believe that today's assassination squad could be the same ETA group that detonated a car bomb in Madrid two months ago, killing five members of the paramilitary police corps, the Civil Guard. Government officials had feared renewed ETA attacks in the days before the election.

The chief target of the attack was thought to be Saenz de Ynestrillas, who was jailed along with a fellow conspirator, Civil Guard Lt. Col. Antonio Tejero for the 1979 plot to overthrow Suarez. After serving his one-year sentence, Tejero led an assault on the parliament in an attempted coup in 1981 and was sentenced to 30 years in prison the following year.

Saenz de Ynestrillas was not involved in that coup attempt, but he gained notoriety once more when his teen-age son was arrested in 1982 and charged with organizing and arming right-wing groups.

The suspected ETA attack was within the pattern of the Basque separatist activities around election time in Spain. ETA consistently has sought to provoke Spain's Army and Civil Guard officers through its attacks on the security services.

The attack came at a time when ETA is conducting a campaign aimed at damaging Spain's tourist industry. During the past month, seven bombs, planted by Basque separatists, have exploded in coastal resorts along the Mediterranean.